ZF2,Doctrine2,Gedmo - SoftDelete JTI实体与关联

时间:2017-08-08 15:05:42

标签: doctrine-orm zend-framework2

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

但是,它不起作用。如果Customer具有@Gedmo\SoftDeleteable,则与User的外键关联失败。如果我也将User实体设为软删除功能,那么CustomerInvoiceAddress之间的关联失败。

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

我认为它可能是配置中的某些内容,虽然拥有read multiple questions和(当然)the docs的SoftDeleteable扩展本身,我还没有#39 ;弄清楚我做错了什么/哪里做错了。

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

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模块配置

'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还提及"过滤器"。如何使用上面的设置在整个模块中实现它们并使用它们?

1 个答案:

答案 0 :(得分:0)

找到答案。我错过了一段配置,而不是(尚)确定它与需要执行以软删除实体的Listener和LifecycleCallback的关系,但完整的配置如下:

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

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

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