0

I have a parent table and a child table. In the child table is a foreign key pointing to the parent table. On the "ondelete" is set to "restrict".

This is correct, but despite doctrine is not able to delete the row, it does not raise an exception when I flush entity. it's possible to intercept this behavior?

<?php
use Doctrine\ORM\Mapping as ORM;

/**
 * Test1
 *
 * @ORM\Table(name="test1")
 * @ORM\Entity
 */
class Test1
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     */
    private $title;


}



use Doctrine\ORM\Mapping as ORM;

/**
 * Test2
 *
 * @ORM\Table(name="test2", indexes={@ORM\Index(name="test2_ibfk_1", columns={"idtxt1"})})
 * @ORM\Entity
 */
class Test2
{
    /**
     * @var integer
     *
     * @ORM\Column(name="idt2", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idt2;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=100, nullable=true)
     */
    private $title;

    /**
     * @var \Test1
     *
     * @ORM\ManyToOne(targetEntity="Test1")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="idtxt1", referencedColumnName="id")
     * })
     */
    private $idtxt1;


}

CREATE TABLE `test1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `test2` (
  `idt2` int(11) NOT NULL AUTO_INCREMENT,
  `idtxt1` bigint(20) NOT NULL,
  `title` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`idt2`),
  KEY `idtxt1` (`idtxt1`),
  CONSTRAINT `test2_ibfk_2` FOREIGN KEY (`idtxt1`) REFERENCES `test1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ciro
  • 771
  • 1
  • 8
  • 30
  • Can you paste the object definitions? Is there any table inheritance? This answer seems similar: http://stackoverflow.com/a/6334710/825364 – Steve Tauber Aug 06 '14 at 15:20
  • possible duplicate of [On delete cascade with doctrine2](http://stackoverflow.com/questions/6328535/on-delete-cascade-with-doctrine2) – Steve Tauber Aug 06 '14 at 15:20
  • i add a code. the question is different. I do not want that the data is deleted. I want that if I try to do this doctrine alerts me that is not possible because it violates a constant integrity – ciro Aug 06 '14 at 16:34

1 Answers1

0

Okay, I think what you are asking is that you can run code based on when someone tries to delete a child entity. Take a look at the event lifecycle. I think you want to add a listener with an onFlash callback. Then you can take a look at what the user is trying to do and throw an exception if need be.

http://docs.doctrine-project.org/en/2.0.x/reference/events.html

Steve Tauber
  • 9,551
  • 5
  • 42
  • 46