[英]MongoDB, Doctrine 2 & Reference One-To-Many
在同一表中但在MongoDB中涉及一對多關聯。
class Component
{
...
/**
* @MongoDB\ReferenceMany(
* discriminatorMap={
* "component"="Component"
* },
* inversedBy="components.id",
* cascade={"persist", "remove", "refresh", "merge"}
* )
*
*/
protected $components;
public function __construct()
{
$this->components = new ArrayCollection();
}
/**
* Add components
*
* @param $component
*/
public function addComponents(Component $component)
{
if(!$this->components->contains($component)){
$this->components->add($component);
}
}
...
}
這沒有問題地將組件關聯起來,我查看了集合並實際上將我關聯了,但是當我嘗試重新獲得組件時,$ this-> components不是ArrayCollection,而是Object Component
有任何想法嗎?
解決了...
/**
* @MongoDB\ReferenceOne(targetDocument="Component", inversedBy="children", cascade={"all"})
*/
public $parent;
/**
* @MongoDB\ReferenceMany(targetDocument="Component", mappedBy="parent", cascade={"all"})
*/
public $children;
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addChild(component $child)
{
if(!$this->children->contains($child)){
$child->parent = $this;
$this->children->add($child);
}
}
/**
* Get children
*
* @return Doctrine\Common\Collections\ArrayCollection $children
*/
public function getComponents()
{
return $this->children;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.