1

I have an entity Image that is in a ManyToMany relation with a Tag entity. I am trying to get the images that are related to the Tag i am fetching from the database in an inner join request as shown bellow:

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();
    }

But when i execute the query i get this error:

    [Semantical Error] line 0, col 12 near 'images FROM App\Entity\Tag': 
    Error: Invalid PathExpression. Must be a StateFieldPathExpression.
Salim Ben Aissa
  • 435
  • 1
  • 6
  • 21

1 Answers1

3

The solution is to add the select at the alias images not the 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();
    }
Salim Ben Aissa
  • 435
  • 1
  • 6
  • 21