php子级联动删除,关于使用doctrine2删除级联

这是简单的例子。联系人具有一对多关联的电话号码。当联系人被删除时,我希望所有与之关联的电话号码也被删除,因此我使用ON DELETE CASCADE。一对多/多对一关系是通过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中实现相同的目的,要获得相同的数据库级“ ON DELETE CASCADE”行为,请使用onDelete =“ CASCADE”选项配置@JoinColumn 。

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);

}

}

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;

}

}

?>

$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 . "
\n";

}

如果你现在这样做

# doctrine orm:schema-tool:create --dump-sql

您将看到将生成与第一个原始SQL示例相同的SQL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值