原则 2,不能从数据库中删除引用
我的博客有 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';
}
类别实体:
命名空间实体\博客;
/**
* @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';
}
我可以完美地添加评论和条目..所以,现在我添加了一个类别实体来对条目进行分类等..所以,学说现在创建了一个多对多表,我也可以毫无问题地添加数据.. .像这样:
$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();
每次加载页面时,它都不会显示任何错误,也不会删除多对多表前面的链接关联
my blog has 3 entities as follows:
entry:
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';
}
category entitiy:
namespace Entities\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';
}
and comments:
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';
}
i can add comments and entries perfectly.. so , now i have added a category entity to categorized the entries etc.. so, doctrine now created a many to many table which i can also add data in without any problems... like this:
$getEntry = $entryRepo->find(1);
$getCat= $this->_doctrine->getReference('\Entities\Blog\Category', cat['id']);
$getEntry->addEntry($getCat);
$this->em->flush();
works!..
but how do i remove this entry i made to the many to many table? i have tried everything...
i looked at the dopctrine 2 documentation, and followed it and still nothing.. no errors too.
i tried:
$getCat= $cateRepo->find(1);
$getEntry = $entryRepo->find(1);
$getEntry->getCategories()->removeElement($getCat);
$this->em->flush();
also tried this on the category entity:
$getCat= $cateRepo->find(1);
$getEntry = $entryRepo->find(1);
$getCat->entries()->removeElement($getEntry);
$this->em->flush();
everytime i load the page, it doesnt show any errors neither it removes the link asscoiation front he many to many table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Doctrine 文档,您需要从两个实体中删除关联。您是否尝试过将两种方法结合起来? IE
According to the Doctrine documentation, you need to remove the association from both entities. Have you tried a combination of both your methods? I.e.