Parent Child Relationship With A Single Entity In Doctrine 2

后端 未结 3 856
离开以前
离开以前 2021-01-12 16:50

I have database table that looks like this:

+----+--------+--------------------+
| id | parent | description        |
+----+--------+--------------------+
|  1 |          


        
3条回答
  •  醉话见心
    2021-01-12 17:47

    This should work:

    children = new ArrayCollection();
        }
    
        // Once you have that, accessing the parent and children should be straight forward 
        // (they will be lazy-loaded in this example as soon as you try to access them). IE:
    
        public function getParent() {
            return $this->parent;
        }
    
        public function getChildren() {
            return $this->children;
        }
    
        // ...
    
        // always use this to setup a new parent/child relationship
        public function addChild(Category $child) {
           $this->children[] = $child;
           $child->setParent($this);
        }
    
        public function setParent(Category $parent) {
           $this->parent = $parent;
        }
    
    }
    

提交回复
热议问题