php - How to Serialize complex entities in symfony2 -
i want serialize vehicle entity has multiple entities associated it.
/** * vehicle * * @orm\table(name="vehicle") * @orm\entity(repositoryclass="mybundle\entity\repository\vehiclerepository") * @gedmo\softdeleteable(fieldname="deletedat", timeaware=false) */ class vehicle { public function __construct(){ $this->car = new arraycollection(); $this->bus = new arraycollection(); $this->truc = new arraycollection(); } //.... } i have tried in controller
$serializer = $this->container->get('serializer'); $reports = $serializer->serialize($vehicle, 'json'); print_r( $reports);exit; return new response($reports); it gives me error
warning: invalid argument supplied foreach()
as new symfony, appreciated.
i had exact same thing @ work few weeks ago when writing data backup/restore script.
in end used jmsserializerbundle awesome.
you can specify output type, , also, importantly depth settings, avoid recursive nesting issues when dealing relationships.
usage straight forward, you'll need add depth annotations in entities , tell serializer use them follows:
entity
/** * @maxdepth(2) * @var \doctrine\common\collections\arraycollection */ private $tags; controller
$this->serializer = $this->getcontainer()->get( 'jms_serializer' ); $output = $this->serializer->serialize( $entities, 'json', serializationcontext::create()->enablemaxdepthchecks() ) you can specify output type. in case used json constructing data file, can output standard array.
hope helps.
Comments
Post a Comment