Best way to convert Doctrine 2 entity persistent collection to array with Zend Framework 2 Best way to convert Doctrine 2 entity persistent collection to array with Zend Framework 2 json json

Best way to convert Doctrine 2 entity persistent collection to array with Zend Framework 2


Not sure about Zend, but in SF2 + Doctrine you can use getValues() method.

$asArray = $persistentCollection->getValues();


I'll decouple the answer in 2 parts, mainly because you haven't clarified enough if you want to solve purely the PersistentCollection or the entire graph serialization.

1- Assuming you already know how to solve Entity's serialization, the PersistentCollection can be turned into an array of Entities by calling toArray() method.

2- Assuming you don't, I suggest you to either use a pre-built library like JMS Serializer.If you want to create your own by hand, use the ClassMetadata instance that can be extracted from EntityManager->getClassMetadata(get_class($someEntity)) and iterating through fieldMappings and associationMappings properties.