【发布时间】:2011-07-18 15:17:57
【问题描述】:
class Lists extends \Entities\AbstractEntity {
/**
* @Id @Column(name="id", type="bigint",length=15)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="\Entities\Users\Usercomments")
* @JoinColumn(name="id", referencedColumnName="id")
*/
protected $comments;
public function getComments() {
return $this->comments;
}
public function addComments($comment) {
$this->comments->add($comment);
}
public function deleteComments(\Entities\Users\Comments $comments) {
$this->comments->removeElement($comments);
}
/** @PreUpdate */
public function updated() {
//$this->updated_at = new \DateTime("now");
}
public function __construct() {
$this->entry = new \Doctrine\Common\Collections\ArrayCollection();
}
}
我有一个由学说创建的多对多表。我可以通过以下方式将 cmets 添加到该表中:
$getList = $this->_lis->findOneBy((array('userid' => $userid)));
$getComments = $this->_doctrine->getReference('\Entities\Users\Comments', $commentid);
$getList->addComments($getComments);
$this->_doctrine->flush();
但我不能删除... 我试过:removeElement 但没有喜悦.. 有人告诉我,我可以在我的数组集合中取消设置某些东西,我不明白...
【问题讨论】:
-
您使用的是哪种参考?如果它使用 ondelete 限制,您将无法删除它。
-
你为什么要问一个问题并发布完全相反的代码?此外,请确保在构造函数中定义
$comments的方式与使用$entry的方式相同。请发布示例代码并显示您的代码正在执行的 SQL。 -
我认为你需要一个多对多连接表。
标签: zend-framework doctrine doctrine-orm