sauvox
sauvox

Reputation: 41

Symfony doctrine find entities with In and NotIn criteria does not work. Can not find entities while using array of entities as a parameter

So i've got a problem and I am not sure if i do something wrong or there is some bug. Working with Symfony 7.2.1

I got two three entities, one of them is a connection.

Ean, Shelf, EanShelfQuantity

So Ean Shelf Quantities got uuid with Ean and Shelf and I am trying to find a Ean Shelf Quantity entities by this: $eanObjects are array of Ean entity objects, $shelves are array of Shelves objects, EanShelfQuantity got columns referenced to ean and shelf.

$criteria = Criteria::create()
                ->andWhere(Criteria::expr()->in('ean', $eanObjects))
                ->andwhere(Criteria::expr()->notIn('shelf', $shelves));
            $match = $this->entityManager->getRepository(EanShelfQuantity::class)->matching($criteria);

And I am getting entities of Ean Shelf Quantity that have shelves that are in array parameter in notIn

So i checked and use pure sql query and everything works fine, but when trying to use criteria, or QueryBuilder in EanShelfQuantityRepository it doesn't work

Upvotes: 0

Views: 34

Answers (1)

Khribi Wessim
Khribi Wessim

Reputation: 722

Try to use Citeria with Scalars by ensuring you are passing scalar values (e.g., IDs) instead of entity objects, as Criteria doesn't always resolve objects correctly in database queries.

$eanIds = array_map(fn($ean) => $ean->getId(), $eanObjects);
$shelfIds = array_map(fn($shelf) => $shelf->getId(), $shelves);

$criteria = Criteria::create()
    ->andWhere(Criteria::expr()->in('ean', $eanIds))
    ->andWhere(Criteria::expr()->notIn('shelf', $shelfIds));

$match = $this->entityManager->getRepository(EanShelfQuantity::class)->matching($criteria);

Upvotes: 0

Related Questions