Reputation: 2827
I'm trying to convert an entity to a associative array.
It seems that the method toArray()
is not available for entity objects.
Reading Symfony doc, it seems I should use the SerializerInterface
.
After enabling it, I can't seems to find the right syntax to convert my entity into an associative array.
Can someone correct my code please?
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
// -----------------------
public function salleAction(Request $request, Projet $projet, SerializerInterface $serializer) {
return this->json(array(
'projet'=>$serializer->serialize($projet, new ObjectNormalizer())
));
}
With the code above, I'm getting this error message
Warning: Illegal offset type in isset or empty
If I replace new ObjectNormalizer()
by 'jsons'
, I'm getting the next error message:
A circular reference has been detected when serializing the object of class "AppBundle\Entity\Projet" (configured limit: 1)
Upvotes: 2
Views: 3112
Reputation: 1909
I suggest you to add the following method to an object, that needs to be converted
public function toArray()
{
return get_object_vars($this);
}
and use it everywhere $array = $projet->toArray();
Upvotes: 6