Error: Invalid PathExpression. Error resolution [Symfony]

This is an article about Symfony and Doctrine.
Each version is as follows.
Symfony 3.4
Doctrine 2.12

Most of the time, Error can be resolved by following the Error message.
This time, it will be a memorandum when the following error is resolved.

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

The above occurred when I specified the foreign key of the table that was joined to SELECT of ORM.

Error: Invalid PathExpression. Error occurred

An error occurred when I ran the code below.
I have selected p.Tag directly on the joined table.
If I think about it now, it’s natural!

// In Product.php Entity
    /**
     * @var \Eccube\Entity\Tag
     *
     * @ORM\ManyToOne(targetEntity="Eccube\Entity\Tag")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="tag_id", referencedColumnName="id", nullable=false)
     * })
     */
    private $Tag;
...

// Query in ProductRepository
    $qb = $this->createQueryBuilder('p')
        ->select([
            'p.name',
            'p.Tag'
        ]);

 

Error: Invalid PathExpression. Error resolution

By using the IDENTITY() function below, it was possible to get it directly.

// Query in ProductRepository
    $qb = $this->createQueryBuilder('p')
        ->select([
            'p.name',
            'IDENTITY(p.Tag) as tag_id'
        ]);

However, I noticed it after it was resolved, but the reason why the above error does not usually appear is that JOIN was properly defined by query.
You can also eliminate the error by writing as follows.

// Query in ProductRepository
    $qb = $this->createQueryBuilder('p')
        ->leftJoin('p.Tag', 't')
        ->select([
            'p.name',
            't.id as tag_id'
        ]);

 

Summary

I was fortunate to discover a new function with an error that occurred in a different way than usual.
I’ve never used IDENTITY() before, but I found it convenient to get it easily.

I referred to the following site. It contains all the information about SELECT.
Doctrine Query Language


Leave a Reply 0

Your email address will not be published.

3 × four =