นี่คือตัวอย่างง่ายๆ ผู้ติดต่อมีหมายเลขโทรศัพท์ที่เกี่ยวข้องหนึ่งถึงหลายหมายเลข เมื่อผู้ติดต่อถูกลบฉันต้องการลบหมายเลขโทรศัพท์ที่เกี่ยวข้องทั้งหมดดังนั้นฉันจึงใช้ ON DELETE CASCADE ความสัมพันธ์แบบหนึ่งต่อหลาย / หลายต่อกลุ่มถูกนำไปใช้กับโดย foreign key ใน phone_numbers
CREATE TABLE contacts
 (contact_id BIGINT AUTO_INCREMENT NOT NULL,
 name VARCHAR(75) NOT NULL,
 PRIMARY KEY(contact_id)) ENGINE = InnoDB;
CREATE TABLE phone_numbers
 (phone_id BIGINT AUTO_INCREMENT NOT NULL,
  phone_number CHAR(10) NOT NULL,
 contact_id BIGINT NOT NULL,
 PRIMARY KEY(phone_id),
 UNIQUE(phone_number)) ENGINE = InnoDB;
ALTER TABLE phone_numbers ADD FOREIGN KEY (contact_id) REFERENCES \
contacts(contact_id) ) ON DELETE CASCADE;
ด้วยการเพิ่ม "ON DELETE CASCADE" ในข้อ จำกัด คีย์ต่างประเทศ phone_numbers จะถูกลบโดยอัตโนมัติเมื่อผู้ติดต่อที่เกี่ยวข้องถูกลบ
INSERT INTO table contacts(name) VALUES('Robert Smith');
INSERT INTO table phone_numbers(phone_number, contact_id) VALUES('8963333333', 1);
INSERT INTO table phone_numbers(phone_number, contact_id) VALUES('8964444444', 1);
ตอนนี้เมื่อลบแถวในตารางที่ติดต่อแล้วแถว phone_numbers ที่เกี่ยวข้องทั้งหมดจะถูกลบโดยอัตโนมัติ
DELETE TABLE contacts as c WHERE c.id=1; /* delete cascades to phone_numbers */
เพื่อให้ได้สิ่งเดียวกันใน Doctrine หากต้องการรับ behavoir DB ON ระดับ "ON DELETE CASCADE" คุณกำหนดค่า @JoinColumn ด้วยตัวเลือกonDelete = "CASCADE"
<?php
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity
 * @Table(name="contacts")
 */
class Contact 
{
    /**
     *  @Id
     *  @Column(type="integer", name="contact_id") 
     *  @GeneratedValue
     */
    protected $id;  
    /** 
     * @Column(type="string", length="75", unique="true") 
     */ 
    protected $name; 
    /** 
     * @OneToMany(targetEntity="Phonenumber", mappedBy="contact")
     */ 
    protected $phonenumbers; 
    public function __construct($name=null)
    {
        $this->phonenumbers = new ArrayCollection();
        if (!is_null($name)) {
            $this->name = $name;
        }
    }
    public function getId()
    {
        return $this->id;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function addPhonenumber(Phonenumber $p)
    {
        if (!$this->phonenumbers->contains($p)) {
            $this->phonenumbers[] = $p;
            $p->setContact($this);
        }
    }
    public function removePhonenumber(Phonenumber $p)
    {
        $this->phonenumbers->remove($p);
    }
}
<?php
namespace Entities;
/**
 * @Entity
 * @Table(name="phonenumbers")
 */
class Phonenumber 
{
    /**
    * @Id
    * @Column(type="integer", name="phone_id") 
    * @GeneratedValue
    */
    protected $id; 
    /**
     * @Column(type="string", length="10", unique="true") 
     */  
    protected $number;
    /** 
     * @ManyToOne(targetEntity="Contact", inversedBy="phonenumbers")
     * @JoinColumn(name="contact_id", referencedColumnName="contact_id", onDelete="CASCADE")
     */ 
    protected $contact; 
    public function __construct($number=null)
    {
        if (!is_null($number)) {
            $this->number = $number;
        }
    }
    public function setPhonenumber($number)
    {
        $this->number = $number;
    }
    public function setContact(Contact $c)
    {
        $this->contact = $c;
    }
} 
?>
<?php
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$contact = new Contact("John Doe"); 
$phone1 = new Phonenumber("8173333333");
$phone2 = new Phonenumber("8174444444");
$em->persist($phone1);
$em->persist($phone2);
$contact->addPhonenumber($phone1); 
$contact->addPhonenumber($phone2); 
$em->persist($contact);
try {
    $em->flush();
} catch(Exception $e) {
    $m = $e->getMessage();
    echo $m . "<br />\n";
}
ถ้าคุณทำตอนนี้ 
# doctrine orm:schema-tool:create --dump-sql
คุณจะเห็นว่า SQL เดียวกันจะถูกสร้างขึ้นในตัวอย่างแรกดิบ SQL