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

SymfonyDoctrine OrmDql

Symfony Problem Overview


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 am trying to run a query like this:

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

$result = $qb->getQuery()->getArrayResult();

However, I am getting the following error:

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

How can I select the parent_id field from my table. I have 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 fields in the table except for the parent_id. This seems like Doctrine is getting in the way. How can I query for this parent_id field? or better yet how can I get all fields in the table including the parent_id

Symfony Solutions


Solution 1 - Symfony

You can use the currently undocumented IDENTITY function to select the FK IDs in a query:

SELECT IDENTITY(c.parent) ...

Solution 2 - Symfony

Solution using createQueryBuilder:

$query->SELECT('pa.id')
    	->from('Category', 'ca');
$query->join('ca.parent', 'pa');

$result = $query->getQuery()->getArrayResult();

Solution 3 - Symfony

You are selecting an object that is not joined. Like said in another answer, you have to do something like :

qb->innerJoin("c.parent", "p")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - SymfonyJani HartikainenView Answer on Stackoverflow
Solution 2 - Symfonyuser3497021View Answer on Stackoverflow
Solution 3 - SymfonyMirza SelimovicView Answer on Stackoverflow