Question

Hi I'm having a bit of trouble with a custom query. What I want to do is this query:

SELECT dp.* FROM `document_piece` dp 
LEFT JOIN document_category dc 
ON dc.id = dp.category_id 
ORDER BY dc.name ;

What I tried so far are some attempts in DQL but since they didn't work I tried using the querybuilder. What I have so far is this:

$q = $this->_em->createQueryBuilder()->select('p', 'c')
            ->from('Core\Entity\DocumentPiece', 'p')
            ->leftJoin('p.category_id', 'c')
            ->orderBy('c.name', 'ASC');

And I'm getting this error [Semantical Error] line 0, col 69 near 'c ORDER BY c.name': Error: Class Core\Entity\DocumentPiece has no association named category_id

Here the (relevant) annotations for the files:

Core\Entity\DocumentPiece

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $id;

/**
 * @var \Core\Entity\DocumentCategory
 *
 * @ORM\ID
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\OneToOne(targetEntity="Core\Entity\DocumentCategory")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 * })
 */
private $category;

Core\Entity\DocumentCategory

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

I hope you can help me on this if I either screwed the annotation or the DQL.

Was it helpful?

Solution

DQL uses objects and properties, not database tables and columns.

->leftJoin('p.category', 'c')  // Lose the _id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top