ZF2, Doctrine2, Gedmo - 软删除具有关联的 JTI 实体



我正在尝试软删除完整的CustomerCustomer扩展UserCustomer还具有关联的InvoiceAddress[]实体。

但是,它不起作用。如果Customer@GedmoSoftDeleteable,则在与User的外键关联上失败。如果我还使User实体软删除,那么它在CustomerInvoiceAddress之间的关联上失败。

如果我将CustomerInvoiceAddress之间的关系设置为cascade={"persist", "remove"}(remove添加(,那么它会硬删除与Customer相关的所有实体。

我认为这可能是配置中的某些内容,尽管阅读了多个问题和(当然(SoftDeleteable 扩展本身的文档,但我还没有弄清楚我做错了什么/在哪里。

下面是我的设置,我已经从代码中删除了与问题无关的内容。

Customer.php

namespace CustomerEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use GedmoSoftDeleteableSoftDeleteable;
// moar
/**
* @ORMTable
* @ORMEntity
*
* @GedmoSoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class Customer extends User implements SoftDeleteable
{
use GedmoDeletedAtTrait;
/**
* @var ArrayCollection|InvoiceAddress[]
* @ORMOneToMany(targetEntity="CustomerEntityInvoiceAddress", mappedBy="customer", cascade={"persist"}, fetch="EAGER")
*/
protected $invoiceAddresses;
// properties, __construct(){}, getters/setters...
}

User.php

namespace UserEntity;
use BjyAuthorizeProviderRoleProviderInterface;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use MvcEntityAbstractEntity;
use ZfcUserEntityUserInterface;
/**
* @ORMTable
* @ORMEntity
* @ORMHasLifecycleCallbacks
*
* @ORMInheritanceType("JOINED")
* @ORMDiscriminatorColumn(name="discr", type="string")
*/
class User extends AbstractEntity implements UserInterface, ProviderInterface
{
// properties, constructor, getters/setters... 
}

GedmoDeletedAtTrait.php

namespace MvcTraits;
use GedmoSoftDeleteableTraitsSoftDeleteableEntity;
trait GedmoDeletedAtTrait
{
use SoftDeleteableEntity;
/**
* Note: overrides Annotation (column name) and type hint, else it's the same as the original
*
* @var DateTime|null
* @DoctrineORMMappingColumn(name="deleted_at", type="datetime", nullable=true)
*/
protected $deletedAt;
}

客户模块的学说模块配置

'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => [
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src'
. DIRECTORY_SEPARATOR . 'Entity',
]
],
'orm_default'             => [
'drivers' => [
__NAMESPACE__ . 'Entity' => __NAMESPACE__ . '_driver'
],
],
],
'eventmanager' => [
'orm_default' => [
'subscribers' => [
SoftDeleteableListener::class,
],
],
],
],

相关问题:文档还提到了"过滤器"。如何通过上述设置实现它们并在整个模块中使用它们?

找到了答案。我缺少一段配置,(尚(不确定它与需要执行以软删除实体的侦听器和生命周期回调的关系,但完整的配置如下:

use GedmoSoftDeleteableFilterSoftDeleteableFilter;
use GedmoSoftDeleteableSoftDeleteableListener;
[ ... ]
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => [
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src'
. DIRECTORY_SEPARATOR . 'Entity',
]
],
'orm_default'             => [
'drivers' => [
__NAMESPACE__ . 'Entity' => __NAMESPACE__ . '_driver'
],
],
],
'eventmanager' => [
'orm_default' => [
'subscribers' => [
SoftDeleteableListener::class,
],
],
],
// THIS IS THE PART THAT WAS MISSING
'configuration' => [
'orm_default' => [
'filters' => [
'soft-deletable' => SoftDeleteableFilter::class,
],
],
],
],

在上面的截图中,我用评论标记了缺失的部分。但是,由于该位只是设置了一个要在别名上使用的过滤器,因此我不确定它与上述定义侦听器的配置有何关系。

如果我以后/将来想通了,我可能会回来更新这个答案。与此同时,也许其他人可能会对信息发表评论?

最新更新