简体   繁体   English

原则2.1-从集合中删除

[英]Doctrine 2.1 - Removing from collection

I am trying to remove comments from a parent entity, I remember doing this on my last website but now it's not working.. 我正在尝试从上级实体中删除评论,我记得在我的上一个网站上这样做,但是现在不起作用。

My entity - users 我的实体-用户

namespace Application\Entities;

use Doctrine\ORM\Mapping AS ORM,
    Doctrine\Common\Collections\ArrayCollection;

/**
 * Loan
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class Users{

    /**
     * @var integer $id
     *
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


    /**
     * @var string $username
     *
     * @ORM\Column(type="string", length=45, nullable=false)
     */
    private $username;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Comments", mappedBy="author", cascade={"persist", "remove"})
     */
    private $comments;

    public function getComments(){

          return $this->comments;              

    }

and my comments table: 和我的评论表:

    namespace Application\Entities;

    use Doctrine\ORM\Mapping AS ORM,
        Doctrine\Common\Collections\ArrayCollection;

/**
 * Loan
 *
 * @ORM\Table(name="comments")
 * @ORM\Entity
 */
class Comments{

    /**
     * @var integer $id
     *
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


    /**
     * @var integer $user_id
     *
     * @ORM\Column(type="integer", length=15, nullable=false)
     */
    private $user_id

    /**
     * @var Loan
     *
     * @ORM\ManyToOne(targetEntity="Users", inversedBy="comments",cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * })
     */
    private $author;

This is fine, it works and I get all collections called comments in the users repository.. 很好,它可以正常工作,并且我在用户存储库中获得了所有称为注释的集合。

Now, I usually do this when I need to delete: 现在,我通常在需要删除时执行此操作:

$commentToDelete = $this->em->getRepository('Entities\Comments')->findOneById(375);
$userResults = $this->em->getRepository('Entities\Users')->findOneById(23);
$userResults->getComments()->removeElement($commentToDelete);
$this->em->flush();

Nothing deletes, neither it throws an exception to tell me it hasn't. 什么都不会删除,也不会抛出异常来告诉我它没有。

I doctrine flushed it too, checked the db, and it's still there.. 我学说也冲洗了它,检查了数据库,它仍然在那里。

UPDATE: 更新:
Straight after I removeElement, I looped through the user id = 23 dataset, and the comment data for id375 is not there... so it removed it from the collection but not from the DB, and I thought $em->flush() is supposed to do this? 在我删除Element之后,我立即遍历了用户id = 23数据集,并且id375的注释数据不存在...因此它从集合中将其删除,但未从数据库中删除,因此我认为$em->flush()应该这样做吗?

Please advise 请指教
Thanks 谢谢

You need to use 您需要使用

$em->remove($commentToDelete);
$em->flush();

Because the mapping is held in the comment you need to remove this entity to remove the reference before you flush which will save the state to the db. 由于映射保留在注释中,因此在刷新之前需要删除该实体以删除引用,这会将状态保存到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM