我在具有大量交叉引用数据的系统中使用Gedmo Doctrine Extensions和SoftDeleteable扩展。我遇到了以下情况:
应该注意的是,我不希望删除级联,这意味着我不想删除产品A ,因为供应商B 已被删除。我只需要在删除供应商记录时将供应商外键设置为NULL(或由JMS解释为NULL)。
这是一种常见的情况,我无法想象其他人没有经历过这种情况,但我没有找到任何答案。有没有人有任何建议?
谢谢, -Nate
答案 0 :(得分:1)
在相同的情况下我是怎么做的:
其中抛出EntityNotFoundException的扩展的Doctrine代理订阅服务器。要覆盖服务,只需添加参数:
jms_serializer.doctrine_proxy_subscriber.class: YourCustom\JMSDoctrineProxySubscriber
在自定义DoctrineProxySubscriber上
<?php
namespace YourCustom;
use Doctrine\ORM\EntityNotFoundException;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber;
class JMSDoctrineProxySubscriber extends DoctrineProxySubscriber
{
public function onPreSerialize(PreSerializeEvent $event)
{
try {
parent::onPreSerialize($event);
} catch (EntityNotFoundException $e) {
// The exception is thrown when soft-deleted objects are tried to be loaded.
// Soft-deleted Doctrine proxy objects should be treated as null. Overriding event type which is eventually
// a class name to stdClass would make it skip and result in as null.
$event->setType('stdClass');
}
}
}
答案 1 :(得分:0)
试试这个:
$product = $em->find('models\ProductA', 1);
$supplier = $product->getSupplier();
$product->setSupplier(null);
$em->remove($supplier);
$em->flush();