如何禁用在 Symfony 4 中返回其他实体引用?
How to disable returning other entities references in Symfony 4?
我在 Symfony 4 中有一些相互关联的实体。
所以基本上一个房子可以有多个卧室和多个厨房。每个厨房可以有多个橱柜。我目前的设计是输入一个卧室 ID,然后查找与该卧室 ID 关联的所有厨房,然后 return 与这些厨房关联的所有橱柜。
这是查询生成器(效果很好):
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();
}
其中return一个如下形状的列表:
[
{
"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 会自动为所有关联添加魔法吸气剂。有没有办法禁用它?只是return这样的橱柜信息:
[
{
"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 试图向我解释如何在 中进行操作。但老实说,我还是不明白。
编辑:我没有分享我的实体代码,因为我认为这在这种情况下并不重要,但如果您需要了解它们的外观,请查看 。
当您使用 ->addSelect('cabinet')
时,您将从 cabinet
获取所有数据。如果您只需要某些属性,请改用以下方式指定它们:
->select('cabinet.id, cabinet.time, cabinet.productCategory')
我在 Symfony 4 中有一些相互关联的实体。
所以基本上一个房子可以有多个卧室和多个厨房。每个厨房可以有多个橱柜。我目前的设计是输入一个卧室 ID,然后查找与该卧室 ID 关联的所有厨房,然后 return 与这些厨房关联的所有橱柜。
这是查询生成器(效果很好):
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();
}
其中return一个如下形状的列表:
[
{
"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 会自动为所有关联添加魔法吸气剂。有没有办法禁用它?只是return这样的橱柜信息:
[
{
"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 试图向我解释如何在
编辑:我没有分享我的实体代码,因为我认为这在这种情况下并不重要,但如果您需要了解它们的外观,请查看
当您使用 ->addSelect('cabinet')
时,您将从 cabinet
获取所有数据。如果您只需要某些属性,请改用以下方式指定它们:
->select('cabinet.id, cabinet.time, cabinet.productCategory')