【发布时间】:2016-06-21 07:59:23
【问题描述】:
我想更改 Sylius ProductBundle 中的 default mapping of the Product entity。所以我为ClassMetadata 事件创建了一个监听器:
<?php
namespace App\Symfony\EventListener\Sylius;
use Sylius\Component\Product\Model\Product;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
/**
* Remove the reference of a Variant from its parent (Product) without delete it
*/
class ProductLoadMetadataSubscriber implements EventSubscriber
{
/**
* @return array
*/
public function getSubscribedEvents()
{
return array(
'loadClassMetadata',
);
}
/**
* @param LoadClassMetadataEventArgs $eventArgs
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
/** @var ClassMetadata $metadata */
$metadata = $eventArgs->getClassMetadata();
if (Product::class !== $metadata->name) {
return;
}
// Property "variants" in "Sylius\Component\Product\Model\Product" was already declared, but it must be declared only once
$metadata->mapOneToMany([
'fieldName' => 'variants',
'targetEntity' => 'Sylius\Component\Product\Model\VariantInterface',
'mappedBy' => 'object',
'orphanRemoval' => false
]);
//$variantsMapping = $metadata->getAssociationMapping('variants');
//$variantsMapping['orphanRemoval'] = false;
}
}
我的目标是将variants 字段的orphanRemoval 选项值设置为false。
但我没有找到更新 ClassMetadataInfo 实例的方法或其他东西。
目前我有这个错误:
“Sylius\Component\Product\Model\Product”中的属性“变体”是 已经声明过,但只能声明一次
这是合乎逻辑的,因为 variants 字段已经存在。
编辑:
我尝试了另一件事但没有成功:
unset($metadata->associationMappings['variants']['orphanRemoval']);
// setAssociationOverride doesn't handle 'orphanRemoval' but it calls internally _validateAndCompleteOneToManyMapping
// which set orphanRemoval to false by default
$metadata->setAssociationOverride('variants', []);
【问题讨论】:
标签: php symfony doctrine-orm sylius