我该如何为儿童做固定器,以保存关系的孩子:
/**
* @ORMManyToMany(targetEntity="User", mappedBy="parents", cascade={"persist"})
*/
private $children;
/**
* @ORMManyToMany(targetEntity="User", inversedBy="children")
* @ORMJoinTable(
* name="family",
* joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="family_user_id", referencedColumnName="id")}
* )
*/
private $parents;
/**
* User constructor.
*/
public function __construct()
{
$this->children = new DoctrineCommonCollectionsArrayCollection();
$this->parents = new DoctrineCommonCollectionsArrayCollection();
}
我的设定器,它不会丢弃错误,也不会保存关系:
/**
* @param mixed $children
*/
public function setChildren($children)
{
$this->children[] = $children;
$children->setParents($this);
}
用户学说保存,关系而不是。
//Methods to add parents and Children
public function addChild(User $child)
{
$this->children->add($child);
$child->addParent($this);
return $this;
}
public function addParent(User $parent)
{
$this->parents->add($parent);
return $this;
}
//Methods to remove parents and children
public function removeChild(User $child)
{
$this->children->removeElement($child);
$child->removeParent($this);
return $this;
}
public function removeParent(User $parent)
{
$this->parents->removeElement($parent);
return $this;
}