php - 教义 : How to unset (SET NULL) OneToMany relationship

标签 php symfony doctrine-orm one-to-many

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

有两个相关的实体(作者和书籍),例如“一位作者拥有零本或多本书籍”和“一本书籍由零位或一位作者拥有”,我试图取消设置一位作者与其其中一位作者之间的关系书籍(来自作者端),期望数据库中的 book.author_id 字段设置为 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;
    }
}

在主 Controller 中我执行以下操作。

  • 检索作者

  • 检索该作者拥有的图书

  • 从作者那里删除书籍

  • 保留作者并刷新

  • 取回书籍

  • 获取作者...

而且作者还是原来的。在数据库中,该记录的字段book.author_id并没有像预期那样设置为NULL,但仍然与作者相关。

Controller 代码:

    $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)的输出中,我们可以看到这本书仍然与作者相关。

当然,我遗漏了一些东西,但我找不到差距在哪里。

最佳答案

如果您想删除作者和图书实体之间的关联,您应该删除图书实体中的关联,并使用 $em->flush() 保留它。 $em->persist($entity) 不用于实体删除。删除 PHP 对象中的关联并通过 ORM 刷新它们,删除数据库中的关联。

只需在 Doctrine 文档中查看即可:

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

我希望它对你有用。干杯!

关于php - 教义 : How to unset (SET NULL) OneToMany relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145326/

相关文章:

php - 通知查询返回新结果或更新结果的有效方法

php - Azure 不会 cURL 谷歌站点搜索 xml

PHP SQL 查询 - 无法添加 HTML 格式

php - Doctrine :创建实体后不保留日期时区

symfony - 如何从 Symfony4 中的数据库加载翻译?

php - 如何在 zf2 中使用户名不区分大小写

MySQL NOW() 在 Doctrine DBAL 插入中?

Php从2个表数据添加mysql

java - 需要一个应用程序来修复带有未转义字符的 XML

mysql - 尝试将 sql 脚本导入现有数据库会生成错误