当由多个表引用时,使用Doctrine删除MySQL中的条目

时间:2013-02-28 01:27:48

标签: mysql doctrine-orm cascade

我有一个数据库模式,其中一个表表示物理地址Addresses,两个表表示,一个表示Manufacturers,另一个表示Vendors。由于我们数据库的性质,ManufacturerVendor可能会引用相同的Address

当我尝试使用Doctrine删除引用VendorAddress引用的Manufacturer时,我的问题就开始了。 ManufacturerVendorOneToOne的关系均为Address

我可以在phpMyAdmin中删除该条目,我在Doctrine中尝试了cascade="remove"orphanRemoval=true选项,但似乎都没有。

有关如何使这项工作的任何想法?这就是我在addressManufacturer中定义Vendor的方式:

/**
 * @ORM\OneToOne(targetEntity="Address", orphanRemoval=true)
 */
private $address;

我不知道为什么当它似乎在phpMyAdmin中正常工作时我无法使用它。这种关系是单向的,因此AddressManufacturerVendor无关。

编辑(显示实际应该从数据库中删除实体的代码): 这是实际删除实体的代码(其中$instance只是一个表示实体的id的整数,$entity是我想要删除的实体类型的名称(例如Address):

//  Grab entity from Doctrine.
$entry = $this->doctrine->em->getRepository($entity)->find($instance);

//  Remove entity from database.
$this->doctrine->em->remove($entry);

//  Flush the entity manager.
$this->doctrine->em->flush();

此代码的结果(当试图删除指向Vendor实例也指向的Address实例的Manufacturer实例时)服务器500错误,其中包含以下内容在error_log中:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`{withheld}`.`Manufacturers`, CONSTRAINT `FK_272810E7F5B7AF75` FOREIGN KEY (`address_id`) REFERENCES `Addresses` (`id`))' in {withheld}/application/libraries/Doctrine/DBAL/Connection.php:754\nStack trace:\n#0 {withheld}/application/libraries/Doctrine/DBAL/Connection.php(754): PDOStatement->execute(Array)\n#1 {withheld}/application/libraries/Doctrine/DBAL/Connection.php(438): Doctrine\\DBAL\\Connection->executeUpdate('DELETE FROM Add...', Array)\n#2 {withheld}/application/libraries/Doctrine/ORM/Persisters/BasicEntityPersister.php(464): Doctrine\\DBAL\\Connection->delete('Addresses', Array)\n#3 {withheld}/application/libraries/Doctrine/ORM/UnitOfWork.php(993): Doctrine\\ORM\\Persisters\\BasicEntityPersister->delete(Object(Proxies\\__CG__\\Entities\\Address))\n#4 {withheld}/application/libraries/Doctrine/ORM/UnitOfWork.php(331): Doctrine\\ORM\\UnitOfWork->executeDeletions(Object(Doctrine\\ORM\\Mapping\\ClassMetadata))\n#5 {withheld}/application/libraries/Doctrine/ORM/EntityManager.php(355): Doctrine\\ORM\\UnitOfWork->commit(NULL)\n#6 {withheld}/application/controllers/ajax.php(70): Doctrine\\ORM\\EntityManager->flush()\n#7 [internal function]: Ajax->delete('vendors', '1')\n#8 {withheld}/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array)\n#9 {withheld}/index.php(202): require_once('{withheld}...')\n#10 {main}\n  thrown in {withheld}/application/libraries/Doctrine/DBAL/Connection.php on line 754

0 个答案:

没有答案