Symfony2: Can’t delete entity with OneToMany association

I have 2 entities: Collection and Object. They are related with each other through another entity CollectionObject. Collection and Object both have OneToMany association with CollectionObject. CollectionObject has ManyToOne relation with both Collection and Object. So here collection can have many objects and objects can be in many collections and, basically CollectionObject is manytomany reference table for collections and objects, but with custom fields.
My problem is I can’t delete Collection entity. I wrote method where I delete collection’s all collection_objects and then delete collection itself, but this doesn’t work.
These are my entities:

Collection

 class Collection
 {

private $id;

private $name;

/**
 * @ORM\ManyToOne(targetEntity="MyMini\UserBundle\Entity\User", inversedBy="collections")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

/**
 *    @ORM\OneToMany(targetEntity="MyMini\CollectionBundle\Entity\CollectionObject", mappedBy="collection", cascade={"all"})
 * @ORM\OrderBy({"date_added" = "desc"})
 */
protected $collection_objects;

/**
 * The constructor to load required resources
 */
public function __construct()
{
    $this->collection_objects = new ArrayCollection();
}

/**
 * @return mixed
 */
public function getUser()
{
    return $this->user;
}

/**
 * @param mixed $user
 */
public function setUser($user)
{
    $this->user = $user;
}

/**
 * Get collection_objects
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getCollectionObjects()
{
    return $this->collection_objects;
}

/*
 * @return array
 */
public function getObjects()
{
    return array_map(
        function ($collection_object) {
            return $collection_object->getObject();
        },
        $this->collection_objects->toArray()
    );
}


/**
 * Add collection_object
 *
 * @param \MyMini\CollectionBundle\Entity\CollectionObject
 * @return Collection
 */
public function addCollectionObject(\MyMini\CollectionBundle\Entity\CollectionObject $collectionObject)
{
    if (!$this->collection_objects->contains($collectionObject)) {
        $this->collection_objects->add($collectionObject);
        $collectionObject->setCollection($this);
    }

    return $this;
}

/**
 * Remove collection_object
 *
 * @param \MyMini\CollectionBundle\Entity\CollectionObject
 */
public function removeCollectionObject(\MyMini\CollectionBundle\Entity\CollectionObject $collectionObject)
{
    $this->collection_objects->remove($collectionObject);
}
 /* other getters and setters*/
}

CollectionObject

 class CollectionObject {

private $id;

/**
 * @ORM\ManyToOne(targetEntity="MyMini\CollectionBundle\Entity\Collection", inversedBy="collection_objects")
 * @ORM\JoinColumn(name="collection_id", referencedColumnName="id")
 */
private $collection;

/**
 * @ORM\ManyToOne(targetEntity="MyMini\ThreedBundle\Entity\Object", inversedBy="collection_objects")
 * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
 */
private $object;

private $date_added;

public function getCollection()
{
    return $this->collection;
}

public function setCollection(Collection $collection = null)
{
    $this->collection = $collection;
    return $this;
}

public function getObject()
{
    return $this->object;
}

public function setObject(Object $object = null)
{
    $this->object = $object;
    return $this;
}
}

Object

 class Object{

protected $id;

protected $name;

 /**
 * @ORM\OneToMany(targetEntity="MyMini\CollectionBundle\Entity\CollectionObject", mappedBy="object", cascade={"all"})
 */
protected $collection_objects;

/**
 * @ORM\ManyToOne(targetEntity="MyMini\UserBundle\Entity\User", inversedBy="objects")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;

 /**
 * Set collection_objects
 *
 * @param \MyMini\CollectionBundle\Entity\CollectionObject $collection_object
 * @return Object
 */
public function addCollectionObject(CollectionObject $collection_object)
{
    if(!$this->collection_objects->contains($collection_object)) {
        $this->collection_objects->add($collection_object);
        $collection_object->setObject($this);
    }
    return $this;
}

/**
 * Get collection_objects
 *
 * @return \MyMini\CollectionBundle\Entity\CollectionObject
 */
public function getCollectionObjects()
{
    return $this->collection_objects;
}

public function removeCollectionObject(CollectionObject $collectionObject)
{
    if ($this->collection_objects->contains($collectionObject)) {
        $this->collection_objects->removeElement($collectionObject);
        $collectionObject->setObject(null);
    }

    return $this;
}

public function getCollections()
{
    return array_map(
        function ($collection_object) {
            return $collection_object->getCollection();
        },
        $this->collection_objects->toArray()
    );
}

/**
 * Set user
 *
 * @param \MyMini\UserBundle\Entity\User $user
 * @return Object
 */
public function setUser(\MyMini\UserBundle\Entity\User $user = null)
{
    $this->user = $user;

    return $this;
}

/**
 * Get user
 *
 * @return \MyMini\UserBundle\Entity\User 
 */
public function getUser()
{
    return $this->user;
}
}

Delete Collection function:

  public function deleteCollection($id){
    $collection = $this->em->getRepository('CollectionBundle:Collection')->find($id);
    $collection_objects = $this->em->getRepository('CollectionBundle:CollectionObject')->findBy(array('collection'=> $collection));
    foreach($collection_objects as $co) {
        $this->em->remove($co);
    }

    if($collection != null) {
        $this->em->remove($collection);
        $this->em->flush();
    }
}

deleteAction in CollectionController

 /**
 * @Route("/delete/{id}", name="collection_delete_collection")
 * @ParamConverter("collection", class="CollectionBundle:Collection")
 */
public function deleteAction(Collection $collection, Request $request)
{

    $this->get('mymini.collection_service')->deleteCollection($collection->getId());
    return $this->redirect($this->generateUrl('collection_list'));
}

Is there anything I am missing? Why my entities are not deleted?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s