I have a problem with serializing entity with many relations using groups. I have a problem with serializing related entities this way.
Let's say I have two entities: Product and related Element.
/**
*
* @Serializer\ExclusionPolicy("none")
*/
class Product {
/**
* Primary key
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Groups({"list","details"})
* @Serializer\Type("integer")
*/
protected $id;
/**
* @Serializer\Groups({"list","details"})
* @Serializer\Type("string")
*/
protected $name;
/**
* @ORM\Column(name="description", type="string", length=4096, nullable=true)
*
* @Serializer\Groups({"details"})
* @Serializer\Type("string")
*/
protected $description;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Madden\ProjectBundle\Entity\ProjectResource", mappedBy="project")
* @Serializer\Groups({"details"})
* @Serializer\Type("ArrayCollection<Element>")
*/
protected $details1;
/**
* Relation to project tasks
* @ORM\OneToMany(targetEntity="Madden\ProjectBundle\Entity\ProjectTask", mappedBy="project")
* @Serializer\Exclude()
* @Serializer\Type("ArrayCollection<Element>")
*/
protected $details2;
...
}
Element entity has a similar structure:
/**
*
* @Serializer\ExclusionPolicy("none")
*/
class Element {
/**
* Primary key
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Groups({"list","details"})
* @Serializer\Type("integer")
*/
protected $id;
/**
* @Serializer\Groups({"list","details"})
* @Serializer\Type("string")
*/
protected $name;
/**
* @ORM\Column(name="description", type="string", length=4096, nullable=true)
*
* @Serializer\Groups({"details"})
* @Serializer\Type("string")
*/
protected $description;
...
}
My problem is that when I'm serializing Product with 'details' group entity I want to serialize only id's of Elements but as you see entity has defined same groups as Product (in case that I would need details of element object) because I want have unified groups on all my entities and prevent making hundreds of groups like 'product_details','element_details', and so on.
Is there a way to eventualy change serialization group when I visit relation or something like that? Handler maybe or something like that?
Regards and thanks for any help
product_detail
/product_list
etc solution, and it was pretty nice, because you always have full control of what is serialized. The downside is obviously how verbose the code gets when serializing multiple classes ... I'd also usexxx_partial
/xxx_full
, instead ofxxx_list
/xxx_details
. – Mcinerney