SoftDelete JTI实体与关联

我试图软删除一个完整的CustomerCustomer扩展UserCustomer还具有关联的InvoiceAddress[]实体。

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

如果我将CustomerInvoiceAddress之间的关系InvoiceAddresscascade={"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' => 'DoctrineORMMappingDriverAnnotationDriver',
            '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,
            ],
        ],
    ],
],

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


找到答案。 我错过了一个配置,并没有确定它是如何与需要执行的Listener和LifecycleCallbacks相关联的,以便软删除一个实体,但是完整的配置如下:

use GedmoSoftDeleteableFilterSoftDeleteableFilter;
use GedmoSoftDeleteableSoftDeleteableListener;

[ ... ]

'doctrine' => [
    'driver' => [
        __NAMESPACE__ . '_driver' => [
            'class' => 'DoctrineORMMappingDriverAnnotationDriver',
            '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,
            ],
        ],
    ],
],

在上面的剪辑中,我用评论标记了缺失的部分。 但是,由于该位只是设置了一个用于别名的过滤器,所以我不确定它与上面定义Listener的配置之间的关系。

如果我在将来/将来想出来,我可能会回来并更新这个答案。 同时,也许其他人可能会对信息发表评论?

链接地址: http://www.djcxy.com/p/65039.html

上一篇: SoftDelete JTI Entity with associations

下一篇: Symfony2: temporarily disable softdelete