다음은 간단한 예입니다. 연락처에는 하나 또는 여러 개의 연결된 전화 번호가 있습니다. 연락처가 삭제되면 관련 전화 번호 도 모두 삭제되기를 원하기 때문에 ON DELETE CASCADE를 사용합니다. one-to-many/many-to-one 관계는 phone_numbers의 외래 키로 구현됩니다. 관련 연락처가 삭제 이 때 외래 키 제약 조건에 "ON은 CASCADE 삭제"를 추가하여
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;
는 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 */
이 behavoir "DELETE CASCADE ON"같은 DB 수준을 얻기 위해, 교리에서 같은 일을 달성하기 위해, 당신은
onDelete = "CASCADE" 옵션으로 @JoinColumn을 구성합니다.
<?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 예에서와 같이 생성됩니다 것을 볼 수? 아마도 Doctrine은 데이터베이스 대신 코드로 처리합니다. – Problematic