在Symfony 4中,我有一些相互关联的实体。
因此,一栋房子基本上可以有多个卧室和多个厨房。每个厨房可以有多个橱柜。我当前的设计是输入一个卧室ID,然后查找与该卧室ID的房屋关联的所有厨房,然后返回与这些厨房相关的所有橱柜。
这是querybuilder(效果很好):
public function findCabinetsByBedroomId(Bedroom $bedroom)
{
return $this->createQueryBuilder('cabinet')
->join('cabinet.kitchen', 'kitchen')
->join('kitchen.house', 'house')
->join('house.bedroom', 'bedroom')
->addSelect('cabinet')
->andWhere('bedroom = :bedroom')
->setParameter('bedroom', $bedroom)
->getQuery()
->getResult();
}
返回以下形状的列表:
[
{
"id": 1,
"time": {
"date": "2019-06-12 11:51:22.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"productCategory": "Big",
"kitchen": {
"__initializer__": null,
"__cloner__": null,
"__isInitialized__": true,
"house": {
"__initializer__": null,
"__cloner__": null,
"__isInitialized__": true,
"kitchen": {},
"bedroom": {},
"id": 555,
"name": "someName"
},
"id": 55,
"name": "kitchen1",
"country": "US"
}
},
{
"id": 8888,
"time": {
"date": "2019-06-12 09:51:22.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"productCategory": "small",
"kitchen": {
"__initializer__": null,
"__cloner__": null,
"__isInitialized__": true,
"house": {
"__initializer__": null,
"__cloner__": null,
"__isInitialized__": true,
"kitchen": {},
"bedroom": {},
"id": 555,
"name": "someName2"
},
"id": 2,
"name": "anotherName",
"country": "UK"
}
}
]
我知道Docker自动为所有关联添加了神奇的吸气剂。有没有办法禁用它?要只返回机柜信息,如下所示:
[
{
"id": 1,
"time": {
"date": "2019-06-12 11:51:22.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"productCategory": "Big",
},
{
"id": 8888,
"time": {
"date": "2019-06-12 09:51:22.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"productCategory": "small",
}
]
用户@alx试图在his comment on my previous question中向我解释如何做。但说实话我还是不明白。
编辑:我没有分享我的实体代码,因为我认为在这种情况下不重要,但是如果您需要了解它们的外观,请仔细检查my previous question。
答案 0 :(得分:1)
使用->addSelect('cabinet')
时,您将从cabinet
获取所有数据。如果只需要某些属性,请通过以下方式指定它们:
->select('cabinet.id, cabinet.time, cabinet.productCategory')