I am using the JMS Serializer bundle to serialize Symfony entities into json. Everything is working fine until I start using the MaxDepth annotation to avoid a deep recursion.
I have an entity called "Category" which has "Subcategories", if I don't make use of MaxDepth annotation when I serialize it, it works perfectly and generates a json object the way it should be with the complete tree of subcategories:
{
"id": 1,
"name": "Category 1",
"subcategories": [{
"id": 3,
"name": "Category 1-1",
"subcategories": [{
"id": 7,
"name": "Category 1-1-1",
"subcategories": []
}]
}, {
"id": 4,
"name": "Category 1-2",
"subcategories": []
}]
}
I would like to have only the first level of subcategories serialized, so I have tried configuring my entity this way:
class Category
{
....
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parentCategory")
* @MaxDepth(1)
*/
private $subcategories;
....
}
But for some reason I don't understand when I enable the maxdepth checks, using the following code:
$serializedObj = $jms->serialize($obj, 'json', SerializationContext::create()->enableMaxDepthChecks());
I get this weird result (no subcategory encoded but it knows that there are two):
{
"id": 1,
"name": "Categoria 1",
"subcategories": [{}, {}]
}
Any idea on what's going on?
Thank you!
MaxDepthannotation does not work (yes I haveserializerEnableMaxDepthChecks=true). - Lord Zed@MaxDepthon the hole class?? - famas23