我有一个数据库模式,其中一张表表示物理地址Addresses
,两张表表示,一张表示Manufacturers
,一张表示Vendors
. 由于我们数据库的性质,aManufacturer
和 aVendor
可能会引用相同的Address
.
当我尝试使用 Doctrine 删除Vendor
引用Address
aManufacturer
也引用的 a 时,我的问题就开始了。 Manufacturer
并且Vendor
每个都有OneToOne
关系Address
。
我可以在 phpMyAdmin 中很好地删除该条目,并且我已经尝试过 Doctrine 中的cascade="remove"
andorphanRemoval=true
选项,但似乎都不起作用。
关于如何使这项工作的任何想法?这就是我address
在两者中定义的方式Manufacturer
和Vendor
:
/**
* @ORM\OneToOne(targetEntity="Address", orphanRemoval=true)
*/
private $address;
我不知道为什么我不能让它在 phpMyAdmin 中工作得很好。该关系是单向的,因此与或Address
没有任何关系。Manufacturer
Vendor
编辑(显示实际上应该从数据库中删除实体的代码):这是实际删除实体的代码(其中$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