Reputation: 1161
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
Upvotes: 1
Views: 1052
Reputation: 737
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.
$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();
Upvotes: 1