Zend framework 原则2从关系数组集合中删除对象

Zend framework 原则2从关系数组集合中删除对象,zend-framework,doctrine,doctrine-orm,Zend Framework,Doctrine,Doctrine Orm,我有一个由条令创建的多对多表。。我可以通过以下方式将注释添加到此表中: class Lists extends \Entities\AbstractEntity { /** * @Id @Column(name="id", type="bigint",length=15) * @GeneratedValue(strategy="AUTO") */ protected $id; /** * @ManyToMany(targetEnt

我有一个由条令创建的多对多表。。我可以通过以下方式将注释添加到此表中:

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();

    }

}
但是我不能删除。。。 我试过:悔恨但没有快乐。。 有人告诉我,我可以在数组集合中取消设置soemthing,我不明白…

您可以在ArrayCollection元素上使用一个简单的PHP“unset()”。最好的方法是在实体类中定义一个新方法

以下是从ArrayCollection属性中删除所有元素的示例:

$getList = $this->_lis->findOneBy((array('userid' => $userid)));

$getComments = $this->_doctrine->getReference('\Entities\Users\Comments', $commentid);

$getList->addComments($getComments);
$this->_doctrine->flush();
您可能还可以定义一个实体方法来删除单个元素:

/**
 * Goes into your Entity class
 * Refers to property Entity::widget
 * @return $this
 */
public function removeAllWidgets()
{
    if ($this->widget) {
        foreach ($this->widget as $key => $value) {
            unset($this->widget[$key]);
        }
    }
    return $this;
}

你用的是什么样的参考资料?如果使用ondelete进行限制,您将无法删除它。为什么您要问一个问题并发布与此相反的代码?另外,确保在构造函数中定义
$comments
,方法与使用
$entry
时相同。请发布示例代码,并显示您的代码正在执行的SQL。我认为您需要一个多个连接表。
/**
 * Goes into your Entity class
 * @param int $elementId
 * @return $this
 */
public function removeOnewidget($elementId)
{
    if (isset($this->widget[$elementId])) {
        unset($this->widget[$elementId]);
    }
    return $this;
}