1

I have the following entity with this rekation:

/**
* Acme\DemoBundle\Entity\Book
*
* @ORM\Table(name="book")
* @ORM\Entity(repositoryClass="Acme\DemoBundle\Repository\BookRepository")  
* @ORM\HasLifecycleCallbacks  
* @UniqueEntity(fields="publickey", groups={"publickey"})
*/
class P1guestlistentry {
/**
 * @var P1guestlistentrystatistic
 *
 * @ORM\OneToOne(targetEntity="P1guestlistentrystatistic", orphanRemoval=true, cascade={"all"}, fetch="EAGER")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="fkstatistic", referencedColumnName="pkId", nullable=false)
 * })
 */
private $fkstatistic;

When I try to remove an object like here:

$this->getEntityManager()->getConnection()->beginTransaction();
try{

     $book = $this->getEntityManager()->getRepository('AchmeDemoBundle:Book')->find(3928);
     $this->getEntityManager()->remove($book);
     $this->getEntityManager()->flush();
     $this->getEntityManager()->getConnection()->commit();         
   }catch(Exception $e){
     $this->getEntityManager()->getConnection()->rollBack();
     echo $e->getMessage();
   }
exit;

I can do whatever I want, I get the following error:

An exception occurred while executing 'DELETE FROM book WHERE pkId = ?' with params {"1":3928}: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (p1.book, CONSTRAINT FK_F51A442F78734022 FOREIGN KEY (fkstatistic) REFERENCES bookstatistic (pkId))

Has anbybody an idea what I'm doing wrong? I tried a lot of methods but nothing helps.

j0k
  • 22,600
  • 28
  • 79
  • 90
Wolf-Tech
  • 1,259
  • 3
  • 18
  • 38
  • What does your associated entity look like? And are you sure you need `orphanRemoval` and all the cascade options? – Ocramius Feb 12 '13 at 11:16
  • the mainproblem is, that i must remove the statistic object too when i remove the book and i thought i can do it whith cascade remove or orphanremoval. How can i show you the associated entity? – Wolf-Tech Feb 12 '13 at 11:33

1 Answers1

2

In case someone runs into a similar issue, here is the solution:

/**
 * @var statistic
 *
 * @ORM\OneToOne(targetEntity="statistic", cascade="ALL")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="fkStatistic", referencedColumnName="pkId", onDelete="SET NULL")
 * })
 */

The onDelete option will remove the relation first and then doctrine will do the cascade operation.

Mick
  • 30,759
  • 16
  • 111
  • 130
Wolf-Tech
  • 1,259
  • 3
  • 18
  • 38