0

I have a few entities that are associated with each other in Symfony 4.

This is it

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.

  • 1
    I'm assuming you already saw this question? https://stackoverflow.com/questions/48569537/symfony-4-serialize-entity-wihout-relations –  Jun 20 '19 at 19:15
  • @ILovePHP Yes, I did thanks. I unfortunately couldn't make much sense out of it for my case. –  Jun 20 '19 at 19:19
  • You can't disable the doctrine proxy objects which is where the __stuff__ is coming from. But you should be able to just serialize it. What exactly are your trying to do with your list of cabinets? – Cerad Jun 20 '19 at 20:02
  • @Cerad Just return them so that I can display them. As in, this is the last step I'll do anything with them. –  Jun 20 '19 at 20:06
  • Still not sure what you are really asking for. If you just want a few cabinet properties then just pull them out and return. Otherwise, drop down to sql. – Cerad Jun 20 '19 at 21:18

1 Answers1

-1

When you use ->addSelect('cabinet') you are getting all the data from cabinet. If you only want certain properties, specify them by instead using:

->select('cabinet.id, cabinet.time, cabinet.productCategory')
Dirk J. Faber
  • 4,360
  • 5
  • 20
  • 58
  • 2
    @Doesitmatter, sorry I misunderstood and updated my answer. – Dirk J. Faber Jun 20 '19 at 21:20
  • 2
    Because this answer is simply wrong. Just stop using php's json_encoding and switch to a better Serializer. – Mike Doe Jun 20 '19 at 21:22
  • 1
    Exactly what I needed. Thank you! –  Jun 20 '19 at 21:22
  • 1
    @emix Not sure why you think this is wrong. It does exactly what I hoped for. It returns only the cabinets information. –  Jun 20 '19 at 21:26
  • 2
    By accident only. And good luck with code maintainability having tens of queries like that. Switch to JMS serializer instead and learn how to serialize objects properly. – Mike Doe Jun 20 '19 at 21:40
  • 2
    @Emix, I don't think this question has anything to do with serializing and simply is about getting some properties. – Dirk J. Faber Jun 20 '19 at 21:49
  • 1
    @emix I see what you mean. I indeed have another controller which retrieve something else where i use the method `findAll()` which then can't be solved using this method because I don't have a queryBuilder for it, which forces me to either make one or indeed use a Serializer. I will check both Symfony Serializer and JMS out in the coming few days. Thanks for your advice. Would you please use the tagging system in SO? I almost missed your comment. –  Jun 20 '19 at 22:02