FORSMILE
JA
Symfony2021/05/11

Error: Invalid PathExpression. Resolving the Error [Symfony]

More often than not, errors can be resolved by following the error message.

Back to Blog

More often than not, errors can be resolved by following the error message.

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

This occurred when I specified a foreign key of a joined table in an ORM SELECT query.

ERROR: INVALID PATHEXPRESSION. WHEN THE ERROR OCCURRED

I was directly selecting `p.Tag` from the joined table.

javascript
// 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 METHOD

By using the `IDENTITY()` function shown below, it was possible to retrieve the value directly.

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

However, I realized after resolving the issue that this error usually doesn't occur because I properly define the JOIN in the query.

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

SUMMARY

It was an error that occurred due to an unusual coding style, and I was fortunate to discover a new function.

I had never used `IDENTITY()` before, but I found it convenient for simple data retrieval.

I referred to the following website, which contains comprehensive information regarding SELECT statements.

Recommended Symfony Books

There aren't many options, and since the framework itself contains a vast amount of information, even introductory books offer a substantial amount of content.

📦
Amazon で関連書籍・ツールを検索
Symfony PHP framework book
Amazonで探す →(アソシエイトリンク)