I have a few entities that are associated with each other in Symfony 4.
So basically a House can have multiple bedrooms and multiple kitchens. Each kitchen can have multiple cabinets. My current design is to enter a bedroom id which will then look for all the kitchens associated with the house of that bedroom id and then return all cabinets associated with these kitchens.
This is the querybuilder (which works great):
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();
}
Which return a list of the following shape:
[
{
"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"
}
}
]
I understand that Docker automatically adds magical getters for all associations. Is there a way to disable that? To just return the cabinets information like this:
[
{
"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",
}
]
The user @alx tried to explain to me how to do it in his comment on my previous question. But I still don't understand it to be honest.
Edit: I didn't share my entities code because I didn't think it is important in this case but if you need to know how they look like, check my previous question kindly.