Symfony 4 InnerJoin错误:无效的PathExpression。必须为StateFieldPathExpression

时间:2019-05-18 00:53:10

标签: php symfony

我有一个与Image实体存在ManyToMany关系的实体Image。我试图在内部联接请求中获取与我从数据库中获取的标签相关的图像,如下所示:

public function findOneByNameWithImages(string $name)
    {
        return $this->createQueryBuilder('t')
            ->addSelect('t.images')
            ->leftJoin('t.images', 'images')
            ->where('t.name = :name')
            ->setParameter('name', $name)
            ->getQuery()
            ->getResult();
    }

但是当我执行查询时,出现此错误:

    [Semantical Error] line 0, col 12 near 'images FROM App\Entity\Tag': 
    Error: Invalid PathExpression. Must be a StateFieldPathExpression.

1 个答案:

答案 0 :(得分:0)

解决方案是将选择添加到别名图像而不是t.images:

public function findOneByNameWithImages(string $name)
    {
        return $this->createQueryBuilder('t')
            ->leftJoin('t.images', 'images')
            ->addSelect('images')
            ->where('t.name = :name')
            ->setParameter('name', $name)
            ->getQuery()
            ->getResult();
    }