iv'e there was a problem with getting the full array (with all the data of the built-in child collections and objects) of my document. My document looks exactly like this:
use Doctrine\Common\Collections\ArrayCollection; class User { protected $id; protected $firstname; protected $lastname; protected $email; protected $address; protected $subscription; public function __construct(array $properties = array()) { $this->email = new ArrayCollection(); $this->address = new ArrayCollection(); $this->subscription = new ArrayCollection(); foreach($properties as $name => $value){ $this->{$name} = $value; } } ...
I need a full array of inline collection to output all the data and render json . My query looks like this:
$query = $this->_dbContainer->getDocumentManager()->createQueryBuilder('User')->field('deletedAt')->exists(false); $result = $query->field('id')->equals($id)->getQuery()->getSingleResult();
For example, if I call the toArray() function as follows:
$array = $result->getSubscription()->toArray(); print_r($array);
Then the output ist just an array at the top level:
[0] => Object Subscription... [1] => Object Subscription... ...
How can I easily get such an array?
[0] => array('subscriptionLabel' => 'value1', 'field' => 'value1', ...) [1] => array('subscriptionLabel' => 'value2', 'field' => 'value2', ...) ...
Are there any recommendations or maybe some missing helper scripts to prevent something ugly like this code (how to handle child → child → child szenarios? Ugly → ugly ugly → ugly ugly ugly ugly → ...)
$example = array(); foreach($result->getSubscription() as $key => $subscription) { $example[$key]['subscriptionLabel'] = $subscription->getSubscriptionLabel(); $example[$key]['field'] = $subscription->getField(); ... }
Thanks a lot Stephan
user544452
source share