我的博客有3个实体,如下所示:
项:
namespace Entities\Blog;
/**
* @Entity(repositoryClass="\Entities\Blog\EntryRepository")
* @Table(name="blog_entry")
* @HasLifecycleCallbacks
*/
class Entry extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @Column(name="permalink", type="string", length=255) */
protected $permalink;
/** @Column(name="title", type="string", length=255) */
protected $title;
/** @Column(name="pub_date", type="datetime") */
protected $pub_date;
/** @Column(name="content", type="text") */
protected $content;
/** @OneToMany(targetEntity="\Entities\Blog\Comment", mappedBy="entry") */
protected $comments;
/**
* @ManyToMany(targetEntity="\Entities\Blog\Category", cascade={"persist", "remove"})
* @JoinColumn(name="id", referencedColumnName="id",onDelete="SET NULL", onUpdate="SET NULL")
*/
protected $entrycategories;
public function addEntry($entry) {
$this->entrycategories->add($entry);
}
public function deleteDiscoverFromCategories($entry) {
$this->entrycategories->removeElement($entry);
}
public function getCategories() {
$this->entrycategories;
}
/** @Column(type="datetime") */
private $created_at;
/** @Column(type="datetime") */
private $updated_at;
/** @PreUpdate */
public function updated()
{
$this->updated_at = new \DateTime("now");
}
public function __construct()
{
$this->entrycategories = new \Doctrine\Common\Collections\ArrayCollection();
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
$this->created_at = $this->updated_at = new \DateTime("now");
}
}
class EntryRepository extends \Entities\PaginatedRepository
{
protected $_entityClassName = 'Entities\Blog\Entry';
}
类别权利:
命名空间实体\ Blog;
/**
* @Entity(repositoryClass="\Entities\Blog\CategoryRepository")
* @Table(name="blog_Ccategory")
* @HasLifecycleCallbacks
*/
class Category extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @Column(name="permalink", type="string", length=255) */
protected $permalink;
/** @Column(name="cat_title", type="string", length=255) */
protected $title;
/** @ManyToMany(targetEntity="\Entities\Blog\Entry", mappedBy="entrycategories", cascade={"all"})
*/
protected $entries;
public function __construct()
{
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
$this->entries = new \Doctrine\Common\Collections\ArrayCollection();
$this->created_at = $this->updated_at = new \DateTime("now");
}
}
class EntryRepository extends \Entities\PaginatedRepository
{
protected $_entityClassName = 'Entities\Blog\Entry';
}
和评论:
namespace Entities\Blog;
/**
* @Entity(repositoryClass="\Entities\Blog\CommentRepository")
* @Table(name="blog_comment")
* @HasLifecycleCallbacks
*/
class Comment extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToOne(targetEntity="\Entities\Blog\Entry")
* @JoinColumn(name="entry_id", referencedColumnName="id")
*/
protected $entry;
/** @Column(name="approved", type="string", length=255) */
protected $approved;
/** @Column(name="title", type="string", length=255) */
protected $title;
/** @Column(name="content", type="text") */
protected $content;
/** @Column(name="pub_date", type="datetime") */
protected $pub_date;
/** @Column(type="datetime") */
private $created_at;
/** @Column(type="datetime") */
private $updated_at;
/** @PreUpdate */
public function updated()
{
$this->updated_at = new \DateTime("now");
}
public function __construct()
{
$this->created_at = $this->updated_at = new \DateTime("now");
}
}
class CommentRepository extends \Entities\PaginatedRepository
{
protected $_entityClassName = 'Entities\Blog\Comment';
}
我可以完美地添加评论和条目..所以,现在我已经添加了一个类别实体来对条目等进行分类..所以,doctrine现在创建了一个多对多的表,我也可以添加数据而没有任何问题。像这样:
$getEntry = $entryRepo->find(1);
$getCat= $this->_doctrine->getReference('\Entities\Blog\Category', cat['id']);
$getEntry->addEntry($getCat);
$this->em->flush();
作品!..
但是如何删除我对多对多表格的这个条目?我试过一切...... 我看了看dopctrine 2的文档,然后一点也没有......也没有错误。
我试过了:
$getCat= $cateRepo->find(1);
$getEntry = $entryRepo->find(1);
$getEntry->getCategories()->removeElement($getCat);
$this->em->flush();
也在类别实体上尝试了这个:
$getCat= $cateRepo->find(1);
$getEntry = $entryRepo->find(1);
$getCat->entries()->removeElement($getEntry);
$this->em->flush();
每次我加载页面时,它都不会显示任何错误,也不会删除链接辅助前面他多对多的表
答案 0 :(得分:1)
根据Doctrine documentation,您需要从两个实体中删除关联。你尝试过两种方法的组合吗?即。
$getCat= $cateRepo->find(1);
$getEntry = $entryRepo->find(1);
// Remove the Category -> Entry association
$getCat->entries()->removeElement($getEntry);
// Remove the Entry -> Category association
$getEntry->getCategories()->removeElement($getCat);
// Persist the changes
$this->em->flush();