3
votes

I have an entity (Item) that has a many to one relationship to another entity (Type) declared here:

     /**
     * @ORM\ManyToOne(targetEntity="Type", inversedBy="item")
     * @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=true)
     * @Expose
     */
    protected $itemType;

And when I serialize items, it includes other attributes on the referenced entity (Type) when all I wanted was just the ID:

item_type: {
     id: 1
},

instead with this:

item_type: {
     id: 1,
     name: "Case & Cover",
     description: "Keep your phone safe with stylish cases and covers"
},

I've read the documentation and tested with groups and maxdepths but no luck, and browsing through issues i've encountered this: https://github.com/schmittjoh/JMSSerializerBundle/issues/61#issuecomment-3297955

is this feature already included?

using "jms/serializer-bundle": "dev-master"

EDIT

this is the sample serialized entity with association(s):

{
    id: 1,
    name: "iPhone 5c Slim Genuine Leather Portfolio Case with Stand - Classic Black",
    price: 29.95,
    description: "...",
    image: "1.jpg",
    item_type: {
        id: 1,
        name: "Case & Cover"
    },
    item_brand: [
        {
            id: 1,
            name: "Apple"
        }
    ]
}

what I really wanted was this(without any extra attributes of an associated entity): (using JMS Serializer since it is an awesome library:) )

{
    id: 1,
    name: "iPhone 5c Slim Genuine Leather Portfolio Case with Stand - Classic Black",
    price: 29.95,
    description: "...",
    image: "1.jpg",
    item_type: {
        id: 1
    },
    item_brand: [
        {
            id: 1
        }
    ]
}
1

1 Answers

1
votes

You have to use the exludePolicy for the entity Type @ExclusionPolicy('all') and add @Expose to the id property.

see the documentation http://jmsyst.com/libs/serializer/master/reference/annotations

Hope it's helpful

Best regard.