学说 2 从关系数组集合中删除对象

发布于 2024-11-25 06:28:02 字数 1307 浏览 0 评论 0原文

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

    }

}

我有一个由学说创建的多对多表..我可以通过以下方式设法向该表添加注释:

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

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

$getList->addComments($getComments);
$this->_doctrine->flush();

但我无法删除... 我尝试过:removeElement 但没有快乐.. 有人告诉我,我可以取消数组集合中的某些内容,但我不明白......

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

    }

}

i have a many to many table create by doctrine.. i can manage to add comments to this table by:

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

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

$getList->addComments($getComments);
$this->_doctrine->flush();

but i cant delete...
i tried: removeElement but no joy..
someone told me that i can unset soemthing in my array collection, i dont get it...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2024-12-02 06:28:02

您可以在 ArrayCollection 元素上使用简单的 PHP“unset()”。执行此操作的最佳位置是在实体类中定义一个新方法。

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

/**
 * 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;
}

您可能还可以定义一个 Entity 方法来删​​除单个元素:

/**
 * 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;
}

You can use a simple PHP "unset()" on the ArrayCollection element. Best place to do this would be to define a new method in your Entity class.

Here is an example which removes all elements from an ArrayCollection property:

/**
 * 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;
}

You could probably also define an Entity method to remove a single element:

/**
 * 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文