如何更新实体的映射信息

时间:2016-06-21 07:59:24

标签: php symfony doctrine-orm sylius

我想从Sylius ProductBundle更改the spread operator。所以我创建了一个<?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; } } 事件的监听器:

orphanRemoval

我的目标是将false字段的variants选项值设置为variants

但是我没有找到更新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', []); 字段已经存在。

编辑:

我尝试了另一件事,但没有成功:

$.ajax({
    url: "testOperation.php",
    async: false,
    data: compareHeure,
    success: function (data, statusRequest) {
})

1 个答案:

答案 0 :(得分:0)

要设置orphanRemoval,您可以尝试以下代码段:

if (SomeClass::class === $metadata->getName()) {
    if (isset($metadata->associationMappings['itemShippingTrackings'])) {
        $metadata->associationMappings['itemShippingTrackings']['orphanRemoval'] = false;
    }
}