这是一个非常非常简单的行为,但是我找不到用教条来实现它的方法。我将解释它如何用两个实体来减少复杂性。
有两个实体(Author和Book)相关,比如"一个作者拥有零本或多本书"answers"一本书由零本或一个作者拥有",我试图解除作者和他的一本书之间的关系(从作者方面),期望那本书。
数据库中的Author_id字段设置为空。实体定义如下:
/**
* 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;
}
}
在主控制器中,我做了以下操作。
检索作者
检索作者拥有的图书
删除作者
Persist Author &冲洗
检索书
And the Author保持不变。在数据库领域的书。此记录的author_id未按预期设置为NULL,但仍与作者相关。
控制器代码:
$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)的输出中,我们可以看到这本书仍然与作者相关。
当然,我错过了一些东西,但我找不到差距在哪里。
如果您想要删除Author和Book实体之间的关联,您应该删除Book实体中的关联,并通过使用$em->flush()将其持久化。$em->persist($entity)不用于实体删除。删除PHP对象中的关联,并通过ORM清除它们,将删除数据库中的关联。
请查看Doctrine的文档:
http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html removing-associations
我希望它对你有用。干杯!