나는 미쳐 가고있다.부모에게서 자식 컬렉션 삭제하기
class Parent {
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
protected $id;
/**
* @OneToMany(targetEntity="Core\Parent\Child", mappedBy="parent", cascade={"persist", "remove"})
*/
protected $children;
public function __construct() {
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getChildren() {
return $this->children->toArray();
}
public function removeAllChildren() {
$this->children->clear();
}
public function addChild($child) {
$this->children->add($child);
}
}
이 아이입니다 :
이
은 부모class Child {
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
protected $id;
/**
* @ManyToOne(targetEntity="Core\Parent", inversedBy="children")
* @JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
}
나 한테 무슨이 부모에 대한 기존의 모든 아이를 삭제됩니다 작동하지 않습니다. 내 컨트롤러에서 :
$parent = $em->getRepository('Core\Parent')->find(1);
$parent->removeAllChildren();
이 시점에서 나는 getChildren()
이라고 부를 수 있으며 그것은 비어 있습니다. 스크립트가 종료되기 전에 나는 또한 다음을 수행합니다. $em->flush();
그러나 데이터베이스 테이블을 점검하고 데이터가 아직 남아 있습니다! 나는 그것을 얻지 못한다. 그리고 그것은 나를 미치게 만든다. 이 부모의 기존 자녀를 모두 삭제하려면 어떻게합니까?
OMG를, 감사합니다! 이거 야. – Vic
그러나 'orphanRemoval = true'는 자식에게 다른 참조가없는 경우에만 작동합니다. 추가 참조를 추가하면 더 이상 작동하지 않습니다. – tobain