Symfony2 and Doctrine - Error: Invalid PathExpression method. Must be a StateFieldPathExpression expression

I have an entity that looks like this:

/** * @Gedmo\Tree(type="nested") * @ORM\Table(name="categories") * @ORM\Entity() */ class Category extends BaseCategory { /** * @ORM\OneToMany(targetEntity="Category", mappedBy="parent") */ protected $children; /** * @Gedmo\TreeParent * @ORM\ManyToOne(targetEntity="Category", inversedBy="children") * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") */ protected $parent; } 

and I'm trying to run a query like this:

 $qb = $this->em->createQueryBuilder() ->select('c.parent') ->from('Category', 'c'); $result = $qb->getQuery()->getArrayResult(); 

However, I get the following error:

 [Semantical Error] ... Error: Invalid PathExpression. Must be a StateFieldPathExpression. 

How to select parent_id field from my table. I tried a bunch of variations, and even if I do something like this:

 $qb = $this->em->createQueryBuilder() ->select('c') ->from('Category', 'c'); 

I get all the fields in the table except for parent_id. The Doctrine seems to interfere. How can I request this parent_id field? or better yet, how can I get all the fields in the table, including parent_id

+52
symfony doctrine2 dql
Jan 08 '13 at
source share
3 answers

You can use the current undocumented IDENTITY function to select FK identifiers in a query:

 SELECT IDENTITY(c.parent) ... 
+155
Jan 08 '13 at 14:16
source share

Solution using createQueryBuilder:

 $query->SELECT('pa.id') ->from('Category', 'ca'); $query->join('ca.parent', 'pa'); $result = $query->getQuery()->getArrayResult(); 
+9
Apr 04
source share

You select an object that is not connected. As another answer says, you should do something like:

 qb->innerJoin("c.parent", "p") 
+2
Sep 24 '16 at 20:11
source share



All Articles