Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 282 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to convert Doctrine 2 entity persistent collection to array with Zend Framework 2

#1
What is best way (easy) to convert Doctrine 2 entity persistent collection to array with Zend Framework 2? I want convert to array and later output it with JsonView. I can write function which use get_object_var but it is a problem when entity has next entity collection.

Regards.
Reply

#2
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](

[To see links please register here]

).
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.
Reply

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

$asArray = $persistentCollection->getValues();
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through