Symfony QueryBuilder andWhere for User-Relation not Working

I want to load items for a given User. Between Item and User lives a One-To-Many relationship. If I use the following, everything is working well:

$result = $em→getRepository(Item::class)→findBy(['user' => $user]);

The $result has 3 entries. Now I have to give additional conditions to the query. So I want to use the queryBuilder insight the repository:

$result = $this->createQueryBuilder('i')
        ->andWhere('i.user = :user')
        ->setParameter('user', $user)
        ->getQuery()
        ->getResult();

The result is empty but the query runs without any errors. Can someone tell me what I'm doing wrong? In both cases the $user is the related App\Entity\User entity.

Upvotes: 0

Views: 881

Answers (1)

I found the solution. It didn't work because of the Uuid. This works:

$result = $this->createQueryBuilder('i')
        ->andWhere('i.user = :user')
        ->setParameter('user', $user->getId()->toBinary())
        ->getQuery()
        ->getResult();

Upvotes: 2

Related Questions