Symfony 原则查找具有 In 和 NotIn 标准的实体不起作用。使用实体数组作为参数时找不到实体

问题描述 投票:0回答:1

所以我遇到了一个问题,我不确定我是否做错了什么或者存在一些错误。 使用 Symfony 7.2.1

我有两个三实体,其中一个是连接。

Ean、货架、EanShelf数量

因此 Ean Shelf Quantities 与 Ean 和 Shelf 一起获得了 uuid,我试图通过以下方式找到 Ean Shelf Quantity 实体: $eanObjects 是 Ean 实体对象的数组, $shelves 是 Shelves 对象的数组, EanShelfQuantity 获取引用 ean 和 Shelf 的列。

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

我正在获取 Ean Shelf Quantity 的实体,这些实体的货架位于 notIn 的数组参数中

所以我检查并使用纯sql查询,一切正常,但是当尝试使用条件或EanShelfQuantityRepository中的QueryBuilder时,它不起作用

symfony doctrine criteria
1个回答
0
投票

尝试将 Citeria 与 Scalars 结合使用,确保传递的是标量值 (例如 ID) 而不是实体对象,因为 Criteria 并不总是能在数据库查询中正确解析对象。

$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);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.