Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-959

Problem deleting when the foreign key is the primary key of the table

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.0-RC2
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      Doctrine 2.0-RC2, Symfony 1.4.8

      Description

      when i wan to delete Element

      $element =  $this->entityManager->find('Element', $id);
      entityManager->remove($entity);
      entityManager->flush();
      

      I have this error "Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails .. CONSTRAINT `Element_ibfk_2` FOREIGN KEY (`id_parent`) REFERENCES `Element` (`id`)."

      Do you have any explanation on this problem please?

      There is my classe :

      <?php
      
      namespace Entities;
      
      /**
       * @Entity
       */
      class Element
      {
          /** @Id @Column(type="integer") @GeneratedValue */
          protected $id;
          
          /**
       	* @Column(type="integer", nullable=false)
       	*/
          protected $num_order;
          
       
       	/**
       	* @Column(type="string", length=50, nullable=false)
       	*/
          protected $name;
          
          /**
           * @Column(type="boolean")
           */
          protected $active;
          
          /**
           * @OneToMany(targetEntity="Entities\Element", mappedBy="parent", cascade={"remove"}, orphanRemoval=true)
           */
          protected $children;
          
          
           /**
           * @ManyToOne(targetEntity="Entities\Element", inversedBy="children")
           * @JoinColumn(name="id_parent", referencedColumnName="id")
           */
          protected $parent;
          
          /**
           * Get id
           *
           * @return integer $id
           */
          public function getId()
          {
              return $this->id;
          }
      
          /**
           * Set num_order
           *
           * @param integer $numOrder
           */
          public function setNumOrder($numOrder)
          {
              $this->num_order = $numOrder;
          }
      
          /**
           * Get num_order
           *
           * @return integer $numOrder
           */
          public function getNumOrder()
          {
              return $this->num_order;
          }
      
          /**
           * Set name
           *
           * @param string $name
           */
          public function setName($name)
          {
              $this->name = $name;
          }
      
          /**
           * Get name
           *
           * @return string $name
           */
          public function getName()
          {
              return $this->name;
          }
      
          /**
           * Set active
           *
           * @param boolean $active
           */
          public function setActive($active)
          {
              $this->active = $active;
          }
      
          /**
           * Get active
           *
           * @return boolean $active
           */
          public function getActive()
          {
              return $this->active;
          }
      
          /**
           * Add children
           *
           * @param Entities\Element $children
           */
          public function addChildren(\Entities\Element $children)
          {
              $this->children[] = $children;
          }
      
          /**
           * Get children
           *
           * @return Doctrine\Common\Collections\Collection $children
           */
          public function getChildren()
          {
              return $this->children;
          }
      
          /**
           * Set parent
           *
           * @param Entities\Element $parent
           */
          public function setParent(\Entities\Element $parent)
          {
              $this->parent = $parent;
          }
      
          /**
           * Get parent
           *
           * @return Entities\Element $parent
           */
          public function getParent()
          {
              return $this->parent;
          }
      }
      

        Issue Links

          Activity

          Hide
          Benjamin Eberlei added a comment -

          Set a join column and have it set onDelete = "SET NULL"

          Show
          Benjamin Eberlei added a comment - Set a join column and have it set onDelete = "SET NULL"

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              Ngoc NGUYEN
            • Votes:
              1 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: