2017-08-08 72 views
0

我試圖軟刪除一個完整的Customer。 A Customer延伸UserCustomer也有關聯的InvoiceAddress[]實體。ZF2,Doctrine2,Gedmo - SoftDelete JTI實體與協會

但是,它不起作用。如果Customer的值爲@Gedmo\SoftDeleteable,則在與外部密鑰關聯User上失敗。如果我也使User實體軟刪除,那麼它在CustomerInvoiceAddress之間的關聯上失敗。

如果我做出cascade={"persist", "remove"}(加removeCustomerInvoiceAddress之間的關係,那麼它很難刪除相關Customer所有實體。

我想它可能是在配置上,雖然有readmultiplequestions和SoftDeleteable擴展本身的​​(當然),我還沒有想出什麼/在哪裏我做錯了。

下面是我的設置,我已經從與問題無關的代碼中刪除了東西。

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還提到 「過濾器」。如何在上面的設置中實現它們並在整個模塊中使用它們?

回答

0

找到了答案。我缺了一塊配置,(還)沒有肯定它是如何涉及監聽和需要執行的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, 
      ], 
     ], 
    ], 
], 

在上面的剪輯中,我用評論標記了缺失的部分。但是,由於該位只是設置了一個用於別名的過濾器,所以我不確定它與上面定義Listener的配置之間的關係。

如果我在將來/我將來想出來,我可能會回來更新這個答案。同時,也許其他人可能會對信息發表評論?