我需要获取Item
的列表,并且在可用时,如果当前CustomerItem
拥有Customer
,则将其关联。
在实体方面,我有3类:
客户
class Customer {
private $id;
private $name;
}
项目
class Item {
private $id;
private $description;
}
CustomerItem
class Item {
private $item; // One way ManyToOne
private $customer; // One way ManyToOne
private $amount;
}
ItemRepository
内部的查询生成器如下所示:
$this->createQueryBuilder('item')
->leftJoin(
ItemCustomer::class,
'ic',
Join::WITH,
'ic.item = item AND ic.customer = :customer'
)
->addSelect('ic');
假设有Item
个 A , B 和 C ,而我们的Customer
拥有5个 A 和3x C ,我希望得到的结果如下:
[
{
"item": { id: "A" , name: "Aaa" },
"customerItem": { item: { id: "A" , name: "Aaa" }, customer : {...}, amount: 5 }
},
{
"item": { id: "B" , name: "Bbb" },
"customerItem": null
},
{
"item": { id: "C" , name: "Ccc" },
"customerItem": { item: { id: "C" , name: "Ccc" }, customer : {...}, amount: 3 }
}
]
但是实际上看起来像:
[
{ id: "A" , name: "Aaa" },
{ item: { id: "A" , name: "Aaa" }, customer : {...}, amount: 5 },
{ id: "B" , name: "Bbb" },
null,
{ id: "C" , name: "Ccc" },
{ item: { id: "C" , name: "Ccc" }, customer : {...}, amount: 3 }
]
我也尝试使用本机查询和ResultSetMappingBuilder
$rsm = new ResultSetMappingBuilder($this->_em);
$rsm->addRootEntityFromClassMetadata(Item::class, 'item');
$rsm->addRootEntityFromClassMetadata(CustomerItem::class, 'ci');
$selectClause = $rsm->generateSelectClause();
$sql = <<<SQL
SELECT $selectClause FROM item
JOIN customer_item ci ON ci.itemId = item.id
AND ci.customerId = :customerId
SQL;
$this->_em->createNativeQuery($sql, $rsm);
但是结果是一样的。
最后,我自己做了映射
$length = \count($results);
for ($i = 0; $i < $length; $i += 2) {
$joinResult[] = [
'item' => $results[$i],
'customerItem' => $results[$i + 1],
];
}
可以提供预期的结果,但这样做时感觉不对。