I have the following structure in my database:
Each House has multiple Bedrooms and multiple Kitchens. Each Kitchen has multiple Cabinets.
Right now, I obtain all the cabinets based on a given Bedroom (I know it's weird). So I enter a Bedroom, It looks the House up, gets all the Kitchens associated with that House, then all Cabinets of those Kitchens. This is the code for it:
public function findCabinetsByBedroom(Bedroom $bedroom)
{
return $this->createQueryBuilder('cabinet')
->join('cabinet.kitchen', 'kitchen')
->join('kitchen.house', 'house')
->join('house.bedroom', 'bedroom')
->select('cabinet')
->andWhere('bedroom = : bedroom')
->setParameter('bedroom', $bedroom)
->getQuery()
->getResult(Query::HYDRATE_OBJECT);
}
I would like to extend my code to contain the Kitchen of each Cabinet and even the House. I managed to get the Kitchen by simply adding ->addSelect('kitchen')
to my code. As so:
public function findCabinetsAndTheirKitchenByBedroom(Bedroom $bedroom)
{
return $this->createQueryBuilder('cabinet')
->join('cabinet.kitchen', 'kitchen')
->join('kitchen.house', 'house')
->join('house.bedroom', 'bedroom')
->select('cabinet')
->addSelect('kitchen')
->andWhere('bedroom = : bedroom')
->setParameter('bedroom', $bedroom)
->getQuery()
->getResult(Query::HYDRATE_OBJECT);
}
But trying to add the House information of the Kitchen doesn't work the same way and I guess it has to do with the fact that the Cabinets have no direct relationship with the House. Xdebug shows the following if I use the latest method (aka findCabinetsAndTheirKitchenByBedroom
):
▼$cabinet = {array} [2]
▼0 = {App\Entity\Cabinet} [4]
id = 1
►time = {DateTime} [3]
productCategory = "someCat"
▼kitchen = {App\Entity\Kitchen} [3]
▼house = {Proxies\__CG__\App\Entity\House} [7]
lazyPropertiesDefaults = {array} [0]
►__initializer__ = {Closure} [3]
►__cloner__ = {Closure} [3]
__isInitialized__ = false
*App\Entity\House*bedroom = null
*App\Entity\House*id = 555
*App\Entity\House*name = null
id = 55
country = "US"
Opposes to this when I use the first one (aka findCabinetsByBedroom
):
▼$cabinet = {array} [2]
▼0 = {App\Entity\Cabinet} [4]
id = 1
►time = {DateTime} [3]
productCategory = "someCat"
▼kitchen = {Proxies\__CG__\App\Entity\Kitchen} [7]
lazyPropertiesDefaults = {array} [0]
►__initializer__ = {Closure} [3]
►__cloner__ = {Closure} [3]
__isInitialized__ = false
*App\Entity\Kitchen*house = null
*App\Entity\Kitchen*id = 55
*App\Entity\Kitchen*country = null
So based on these result I concluded that addSelect
indeed returned the Kitchen. And yes I checked the data in the database, it's the correct results. But how would I add the House information to the Cabinet?
One more issue, even though Xdebug shows the correct Kitchen info for each Cabinet, they're for some reason not returned when testing with POSTMAN or the browser. I get the following result:
{ "id": 1, "time": { "date": "2019-06-12 11:51:22.000000", "timezone_type": 3, "timezone": "UTC" }, "productCategory": "someCat", "kitchen": {} }
So it's empty for some reason. Any ideas how to display the information inside the Object Kitchen? I thought it had to do with it being a different object than Cabinet, but following this logic the content of time
should have been empty as well since it's a DateTime
object. So that can't be it but I have no clue why it's returned empty.