ZF2、Doctrine2、Gedmo - 具有关联的 SoftDelete JTI 实体

ZF2, Doctrine2, Gedmo - SoftDelete JTI Entity with associations

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

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

如果我将 CustomerInvoiceAddress 之间的关系设为 cascade={"persist", "remove"}(添加 remove),那么它会很难删除与 [=14= 相关的所有实体].

我认为它可能是配置中的某些东西,虽然有 questions and (of course) the docs 的 SoftDeleteable 扩展本身,但我还没有弄清楚 what/where 我做错了什么。

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

Customer.php

namespace Customer\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\SoftDeleteable\SoftDeleteable;
// moar

/**
 * @ORM\Table
 * @ORM\Entity
 *
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class Customer extends User implements SoftDeleteable
{
    use GedmoDeletedAtTrait;

    /**
     * @var ArrayCollection|InvoiceAddress[]
     * @ORM\OneToMany(targetEntity="Customer\Entity\InvoiceAddress", mappedBy="customer", cascade={"persist"}, fetch="EAGER")
     */
    protected $invoiceAddresses;

    // properties, __construct(){}, getters/setters...
}

User.php

namespace User\Entity;

use BjyAuthorize\Provider\Role\ProviderInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Mvc\Entity\AbstractEntity;
use ZfcUser\Entity\UserInterface;

/**
 * @ORM\Table
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 *
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 */
class User extends AbstractEntity implements UserInterface, ProviderInterface
{
    // properties, constructor, getters/setters... 
}

GedmoDeletedAtTrait.php

namespace Mvc\Traits;

use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;

trait GedmoDeletedAtTrait
{
    use SoftDeleteableEntity;

    /**
     * Note: overrides Annotation (column name) and type hint, else it's the same as the original
     *
     * @var \DateTime|null
     * @Doctrine\ORM\Mapping\Column(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,
            ],
        ],
    ],
],

相关问题:the docs也提到"filters"。如何使用上述设置实现它们并在整个模块中使用它们?

找到答案了。我遗漏了一段配置,(还)不确定它与需要执行以软删除实体的 Listener 和 LifecycleCallbacks 有何关系,但完整的配置如下:

use Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter;
use Gedmo\SoftDeleteable\SoftDeleteableListener;

[ ... ]

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

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

如果我弄明白了 later/in 未来我可能会回来更新这个答案。与此同时,也许其他人可能会对信息发表评论?