iv收到我的文档的完整数组(带有的所有嵌入式子项集合和对象的所有数据)时遇到问题。我的文档看起来就像这样:

use Doctrine\Common\Collections\ArrayCollection;

/** @Document(collection="user") */
class User {

/** @Id */
protected $id;

/** @String */
protected $firstname;

/** @String */
protected $lastname;

/** @EmbedMany(targetDocument="Email") */
protected $email;

/** @EmbedMany(targetDocument="Address") */
protected $address;

/** @EmbedMany(targetDocument="Subscription") */
protected $subscription;


/**
* Construct the user
*
* @param   array $properties
* @throws  User_Exception
*/
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;
    }

}


...

我需要一个完整的嵌入式集合数组来输出全部数据,并通过json 将其呈现。我的查询如下所示:
$query = $this->_dbContainer->getDocumentManager()->createQueryBuilder('User')->field('deletedAt')->exists(false);
$result = $query->field('id')->equals($id)->getQuery()->getSingleResult();

例如,如果我这样调用toArray()函数:
$array = $result->getSubscription()->toArray();
print_r($array);

然后输出ist只是顶层的一个数组:
[0] => Object Subscription...
[1] => Object Subscription...
...

我如何轻松获得这样的数组?
[0] => array('subscriptionLabel' => 'value1', 'field' => 'value1', ...)
[1] => array('subscriptionLabel' => 'value2', 'field' => 'value2', ...)
...

是否有任何最佳做法或可能缺少一些帮助程序脚本来防止类似此代码的丑陋行为(如何处理子->子->子szenarios?
$example = array();
foreach($result->getSubscription() as $key => $subscription) {
    $example[$key]['subscriptionLabel'] = $subscription->getSubscriptionLabel();
    $example[$key]['field'] = $subscription->getField();
    ...
}

非常感谢,
史蒂芬(Stephan)

最佳答案

该死的简单答案!只需使用-> hydrate(false)就可以了。


<?php

$users = $dm->createQueryBuilder('User')
    ->hydrate(false)
    ->getQuery()
    ->execute();

print_r($users);

关于mongodb - Doctrine ODM(MongoDB)-获取对象的完整数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6155189/

10-14 19:33
查看更多