2

以下是主文档字段的定义:

/**
 * @var ArrayCollection
 * @MongoDB\ReferenceMany(
 *      targetDocument="Some\Namespace\Document\Reference",
 *      sort={"creationDate": "desc"},
 *      simple=true
 * )
 * @Expose
 * @Groups({"Main"})
 * @Type("ArrayCollection<Some\Namespace\Document\Reference>")
 * @var \Some\Namespace\Document\Reference[]
 */
protected $references;

我试图获取主要文档的列表并通过 JMS Serializer 对它们进行序列化,但我发现引用是空数组。经过一番调查,我发现,对于 getReferences,文档返回PersistentCollection的实例:

  • 计数返回 2 [好的]
  • getMongoData 返回 MongoIds 数组 [ok]
  • toArray 返回空数组 [无效]

看起来那是因为初始化方法,它清除了 mongoData。

我使用以下代码获得了正确的结果:

/**
 * @VirtualProperty
 * @SerializedName("reference_ids")
 * @Groups("Main")
 * @return array
 */
public function getReferenceIds()
{
    $out = array();

    foreach ($this->getReferences()->getMongoData() as $val) {
        $out[] = (string)$val;
    }

    return $out;
}

但这只是一个捷径,我不认为这是一个正确的解决方案。

如果有人知道如何使用 PersistentCollection 检索这些 id 或整个文档,为什么初始化方法会清除 mongoData ?

谢谢。

4

0 回答 0