【问题标题】:MongoDB, Doctrine 2 & Reference One-To-ManyMongoDB,学说 2 和参考一对多
【发布时间】:2013-01-27 11:22:14
【问题描述】:

是关于同一个表中的一对多关联,但在 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

有什么想法吗?

【问题讨论】:

    标签: php mongodb reference doctrine one-to-many


    【解决方案1】:

    已经解决了……

    /**
     * @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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2013-03-22
      相关资源
      最近更新 更多