SoftDelete JTI Entity with associations

I'm trying to soft delete a complete Customer . A Customer extends User . Customer also has associated InvoiceAddress[] entities.

It however, does not work. If the Customer has @GedmoSoftDeleteable , it fails on the Foreign Key association with User . If I also make the User entity soft delete-able, then it fails on the association between Customer and InvoiceAddress .

If I make the relation between Customer and InvoiceAddress to cascade={"persist", "remove"} (added remove ), then it hard deletes all entities related to the Customer .

I figure it might be something in the configuration, though having read multiple questions and (of course) the docs of the SoftDeleteable extension itself, I haven't figured out what/where I'm doing something wrong.

Below is my setup, I've removed stuff from the code unrelated to the question.

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 module config for Customer module

'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,
            ],
        ],
    ],
],

Related question: the docs also mention "filters". How to implement them and use them throughout a module with the setup above?


Found the answer. I was missing a piece of configuration, not (yet) sure as to how it relates to the Listener and the LifecycleCallbacks that need to be executed to soft-delete an Entity, but the complete configuration is as follows:

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,
            ],
        ],
    ],
],

In the above snipped I've marked the missing piece with a comment. However, as that bit just sets a filter to use on an alias, I'm not sure how it relates to the configuration above, which defines a Listener.

If I figure it out later/in the future I might come back and update this answer. In the mean time, maybe someone else might place a comment with the info?

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

上一篇: 仍然会在除了一个的所有上下文中返回已删除的实体

下一篇: SoftDelete JTI实体与关联