嗨,我有以下类
namespace MPUserRegistrationBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonPersistencePersistentObject;
use MPServicesSiteAdapterBundleUtilString;
/**
* @ORMTable(name="customer")
* @ORMEntity(repositoryClass="MPUserRegistrationBundleRepositoriesCustomerRepository")
* @ORMHasLifecycleCallbacks
*/
class Customer extends PersistentObject
{
/**
* @var string $id
* @ORMId
* @ORMColumn(name="icustomer_id", type="integer")
*/
protected $id;
/**
* @var string $addresses
* @ORMOneToMany(targetEntity="MPUserRegistrationBundleEntityAddress", mappedBy="customer", cascade={"remove"})
*/
protected $addresses;
具有以下关系
/**
* MPUserRegistrationBundleEntity
*/
namespace MPUserRegistrationBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonPersistencePersistentObject;
/**
* @ORMTable(name="custdeladd")
* @ORMEntity(repositoryClass="MPUserRegistrationBundleRepositoriesAddressRepository")
*/
class Address extends PersistentObject
{
/**
* @var integer $suffix
* @ORMColumn(name="isuffix", type="integer")
* @ORMId
*/
protected $suffix;
/**
* @var object $customer
* @ORMManyToOne(targetEntity="MPUserRegistrationBundleEntityCustomer", inversedBy="addresses", cascade={"persist"})
* @ORMJoinColumn(name="icustomer_id", referencedColumnName="icustomer_id")
*/
protected $customer;
}
有谁知道为什么当客户被删除时,地址没有?非常感谢
你的关系定义似乎很好。删除客户的方式是什么?我的意思是 Doctrine 不会直接在数据库中设置"ON DELETE CASCADE"。因此,如果您以"原则"以外的其他方式删除客户实体,则不会删除评论。
你可以告诉教义直接在数据库中设置它,方法是添加注释:
@ORMJoinColumn(name="icustomer_id", referencedColumnName="icustomer_id", onDelete="CASCADE")
但是,如果您尝试以正确的教义方式删除实体,这仍然不起作用,请尝试将"orphanRemoval"添加到 true,它应该会有所帮助:
// Customer.php
/**
* @var string $addresses
* @ORMOneToMany(targetEntity="MPUserRegistrationBundleEntityAddress", mappedBy="customer", cascade={"remove"}, orphanRemoval=true)
*/
protected $addresses;
我在让它工作时遇到了很多麻烦。以下是一些可能有助于那些遇到类似麻烦的人的要点:
- 拥有实体需要
@OneToMany( ... cascade={"remove"}
或cascade={"all"} )
- 子实体还需要
@JoinColumn(... onDelete="CASCADE")
- 此外,如果您要修改
onDelete="CASCADE"
部分,我相信您需要在更改生效之前更新架构。
我尝试了上面的答案,但得到了相同的外键约束违规。
我的代码如下所示:
class FooBar
{
/**
* @ORMOneToMany(
* targetEntity="Foo",
* mappedBy="foobar",
* cascade={"persist", "remove"},
* orphanRemoval=true
* )
* @var Collection|Foo[]
*/
private $fooCollection;
/**
* @return Collection|Foo[]
*/
public function getFooCollection()
{
return $this->fooCollection;
}
/**
* @param Collection|Foo[] $fooCollection
*
* @return $this
*/
public function setFooCollection($fooCollection): FooBar
{
$this->fooCollection = $fooCollection;
return $this;
}
}
class Foo
{
// ... some more properties & ID here ....
/**
* @ORMOneToMany(
* targetEntity="Bar",
* mappedBy="foo",
* cascade={"persist", "remove"},
* orphanRemoval=true
* )
* @var Collection|Bar[]
*/
private $barCollection;
/**
* @ORMManyToOne(
* targetEntity="FooBar",
* inversedBy="fooCollection"
* )
* @ORMJoinColumn(
* name="fooBarId",
* referencedColumnName="fooBarId",
* nullable=false
* )
* @var FooBar
*/
private $fooBar;
public function __construct()
{
$this->barCollection = new ArrayCollection();
}
/**
* @return Bar[]|Collection
*/
public function getBarCollection()
{
return $this->barCollection;
}
/**
* @param Bar[]|Collection $barCollection
*
* @return $this
*/
public function setBarCollection($barCollection): Foo
{
$this->barCollection = $barCollection;
return $this;
}
/**
* @return FooBar
*/
public function getFooBar(): FooBar
{
return $this->fooBar;
}
/**
* @param FooBar $fooBar
*
* @return $this
*/
public function setFooBar(FooBar $fooBar): Foo
{
$this->fooBar = $fooBar;
return $this;
}
// ... some more getters & setters here ...
}
class Bar
{
// ... some more properties & ID here ....
/**
* @ORMManyToOne(
* targetEntity="Foo",
* inversedBy="barCollection"
* )
* @ORMJoinColumn(
* name="fooId",
* referencedColumnName="fooId",
* nullable=false
* )
* @var Foo
*/
private $foo;
// ... some more getters & setters here ...
}
我有程序的另一部分,我试图使用以下代码删除与 FooBar 对应的所有 Foo。
$fooBar = new FooBar();
$fooBar->setFooCollection([]);
$entityManager->persist($foorBar);
$entityManager->flush();
这在 Foo 和 Bar 之间的关系上给出了外键异常"无法删除或更新父行:外键约束失败"。
将以下方法添加到 FooBar:
/**
* @param Foo[] $collection
*/
public function removeFooCollection($collection)
{
/** @var Foo $entry */
foreach ($collection as $entry)
{
$this->fooCollection->removeElement($entry);
$entry->setFooBar(null);
}
}
并使用以下代码删除与 FooBar 相关的所有 Foo:
$fooBar->removeFooCollection(
$fooBar->getFooCollection()
);
$entityManager->persist($fooBar);
$entityManager->flush();
修复了所有外键约束问题。