1
votes

I have a Product entity, and a ProductList entity.

Is it possible to serialize (and deserialize) a ProductList object to json in a way that the json contains the Product objects related to that ProductList?

The expected output is:

[{
    'product_list_name': 'List',
    'product_list_created': '2013-07-04',
    'products' : {
        'product': {...},
        'product': {...},
        'product': {...},
        'product': {...}
}] 

I'm using the Symfony2 built-in serializer and JMS\Serializer but I am not having any luck.

Any way to do this?

1
You want to serialize a entity collection like that ? : [{ 'product_list': { 'product': {...}, 'product': {...}, 'product': {...} }]rpg600
Can you add your entities with your config (annotations / yml / xml) - are they actually linked via a relationship ? im using the JMSSerializer and its working fine for meManse
@Ren Kind of. [{'product_list_name': 'List', 'product_list_created': '2013-07-04', 'products' {'product': {...}, 'product': {...}, 'product': {...}, 'product': {...} }]gomman
@ManseUK Did not do it for space saving, they are well linked, ManyToMany relationship, I load fixtures and create lists, add products, etc, no problem. Is serializing that bothers me.gomman
Ok on your ProductList class try to add the XmlList annotation to his Product attribute, look here jmsyst.com/libs/serializer/master/reference/annotations#xmllist, also add a XmlRoot annotation to tell that ProductList is the parent object.rpg600

1 Answers

0
votes

If you are using YML, ensure you have a YML file for both Product and ProductList.

Entity.ProductList.yml

AppBundle\Entity\ProductList:
    exclusion_policy: ALL
    properties:
        products:
            expose: true

Entity.Product.yml

AppBundle\Entity\Product:
    exclusion_policy: ALL
    properties:
        id:
            expose: true