原则:如何取消(设置为NULL)OneToMany关系 [英] Doctrine: How to unset (SET NULL) OneToMany relationship

查看:76
本文介绍了原则:如何取消(设置为NULL)OneToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常非常简单的行为,但是我找不到用教义实现它的方法.我将解释它仅用两个实体就降低了复杂性.

This is a very very simple behavior, but I can't find the way to achieve it with doctrine. I'm going to explain it reducing the complexity with only two entities.

具有两个相关的实体(作者和书),例如一个作者拥有零本或更多本书"和一本书归零或一个作者所有",我试图打破一个作者与其一个作者之间的关系.图书(从作者那边),希望数据库中的book.author_id字段设置为null.

Having two entites (Author and Book) related like "One Author owns zero or more books" and "One book is owned by zero or one author", I'm trying to unset the relation between an author and one of his books (from the author side), expecting that book.author_id field in database sets as null.

实体定义如下:

作者

/**
 * Author
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Poc\PocBundle\Entity\AuthorRepository")
 */
class Author
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Author
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @ORM\OneToMany(targetEntity="Book", mappedBy="author", cascade={"persist", "remove"})
     */
    private $books;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * Add books
     *
     * @param \Poc\PocBundle\Entity\Book $books
     * @return Author
     */
    public function addBook(\Poc\PocBundle\Entity\Book $books)
    {
        $this->books[] = $books;

        return $this;
    }

    /**
     * Remove books
     *
     * @param \Poc\PocBundle\Entity\Book $books
     */
    public function removeBook(\Poc\PocBundle\Entity\Book $books)
    {
        $this->books->removeElement($books);
    }

    /**
     * Get books
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getBooks()
    {
        return $this->books;
    }
}

图书

/**
 * Book
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Poc\PocBundle\Entity\BookRepository")
 */
class Book
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Book
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @ORM\ManyToOne(targetEntity="Author", inversedBy="books")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id", onDelete="SET NULL")
     */
     protected $author;


    /**
     * Set author
     *
     * @param \Poc\PocBundle\Entity\Author $author
     * @return Book
     */
    public function setAuthor(\Poc\PocBundle\Entity\Author $author = null)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return \Poc\PocBundle\Entity\Author 
     */
    public function getAuthor()
    {
        return $this->author;
    }
}

在主控制器中,执行以下操作.

In the main controller I do the following.

  • 检索作者

  • Retrieve an author

检索该作者拥有的书

从作者删除书

持之以恒的作者&冲洗

Persist Author & flush

检索书

获取作者...

作者保持不变.在数据库中,该记录的字段book.author_id并未按预期设置为NULL,但仍与作者相关.

And the Author remains being the same. In database the field book.author_id of this record is not set to NULL as expected, but is still related with the author.

控制器代码:

    $Book = $this->getDoctrine()
        ->getRepository('PocPocBundle:Book')
        ->find('1');
    $Author = $this->getDoctrine()
        ->getRepository('PocPocBundle:Author')
        ->find('1');

    $Author->removeBook($Book);


    $em = $this->getDoctrine()->getManager();
    $em->persist($Author);
    $em->flush();

    echo "<pre>";
    $Book = $this->getDoctrine()
        ->getRepository('PocPocBundle:Book')
        ->find('1');
    \Doctrine\Common\Util\Debug::dump($Book);

    die;

...和输出

object(stdClass)[286]
      public '__CLASS__' => string 'Poc\PocBundle\Entity\Book' (length=25)
      public 'id' => int 1
      public 'name' => string 'El Quijote' (length=10)
      public 'author' => 
        object(stdClass)[293]
          public '__CLASS__' => string 'Poc\PocBundle\Entity\Author' (length=27)
          public '__IS_PROXY__' => boolean true
          public '__PROXY_INITIALIZED__' => boolean true
          public 'id' => int 1
          public 'name' => string 'Cervantes' (length=9)
          public 'books' => string 'Array(1)' (length=8)

在实体Book(id = 1)的输出中,我们可以看到这本书仍然与作者有关​​.

In the output of entity Book (id=1) we can see this books is still related with the author.

当然,我缺少了一些东西,但我找不到差距.

Sure, I'm missing something but I can't find where is the gap.

推荐答案

如果要删除作者和Book实体之间的关联,则应删除Book实体中的关联,并使用$ em-将其持久化> flush(). $ em-> persist($ entity)不用于实体删除.删除PHP对象中的关联,然后通过ORM刷新它们,从而删除数据库中的关联.

If you want to remove the association between the Author and the Book entities, you should delete the association in your Book entity and persist it too by using $em->flush(). $em->persist($entity) is not used for entity removal. Deleting the association in your PHP objects and flush them through the ORM remove the association in the database.

只需在教义的文档中进行检查,即可:

Just check this in the Doctrine's documentation:

http://docs. doctrine-project.org/en/latest/reference/working-with-associations.html#removing-associations

我希望它对您有用.干杯!

I hope it will work for you. Cheers!

这篇关于原则:如何取消(设置为NULL)OneToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆