Symfony 2 – Doctrine – oneToMany : delete from many-Collection of common programming errors

I have two entities : Episode and Version. When I trie to delete a version a get an exception:

Notice: Undefined index: episode in /var/www/Mendrock/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1561

I already check relations but I don’t found the error.

My entites: Episode

namespace Mendrock\Bundle\SagaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Mendrock\Bundle\SagaBundle\Entity\Episode
 *
 * @ORM\Table(name="episode")
 * @ORM\Entity(repositoryClass="Mendrock\Bundle\SagaBundle\Repository\EpisodeRepository")
 */
class Episode {

    ...

    /**
     * 
     * @ORM\OneToMany(targetEntity="Version", mappedBy="episode", cascade={"remove", "persist"})
     */
    private $versions;

Version

namespace Mendrock\Bundle\SagaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Mendrock\Bundle\SagaBundle\Entity\EpisodeVersion
 *
 * @ORM\Table(name="version")
 * @ORM\Entity(repositoryClass="Mendrock\Bundle\SagaBundle\Repository\EpisodeVersionRepository")
 */
class Version {

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Episode", inversedBy="versions", cascade={"persist"})
     * @ORM\JoinColumn(name="episode_id", referencedColumnName="id")
     */
    private $episode;

    ...

** Controler **

    /**
    *
    * @Route("/versionConfirmeDelete/{id}", options={"expose"=true})
    * @Method("GET")
    * @Template()
    */
    public function versionConfirmeDeleteAction($id) {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MendrockSagaBundle:Version')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Version entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity' => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
    * Deletes a Episode entity.
    *
    * @Route("/versionDelete/{id}")
    * @Method("POST")
    */
    public function versionDeleteAction(Request $request, $id) {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('MendrockSagaBundle:Version')->find($id);
            $episode = $entity->getEpisode();
            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Version entity.');
            }

            $em->remove($entity);
            $em->flush();
            $this->get('session')->setFlash('success', 'La version "' . $entity . '" de l\'épisode "'.$episode.'" à été supprimé !');
        } else {

            $this->get('session')->setFlash('error', 'Impossible de supprimer la version "' . $entity . '".');
        }

        return $this->redirect($this->generateUrl('mendrock_saga_default_episodes'));
    }
    #################################################################
    ### Commun     ##################################################
    #################################################################

    private function createDeleteForm($id) {
        return $this->createFormBuilder(array('id' => $id))
                        ->add('id', 'hidden')
                        ->getForm()
        ;
    }

Any ideas of my problem ?

  1. I found the error, it was an other relation for this entities who make problem. Name of attribut was not corresponded.

Originally posted 2013-11-27 11:53:04.