【问题标题】:Doctrine 2 overriding many to one association原则 2 凌驾于多对一关联之上
【发布时间】:2016-09-20 15:52:25
【问题描述】:

是否可以覆盖@ManyToOne(targetEntity)

我阅读了this Doctrine documentation page,但没有提到如何覆盖targetEntity

这是我的代码:

namespace AppBundle\Model\Order\Entity;

use AppBundle\Model\Base\Entity\Identifier;
use AppBundle\Model\Base\Entity\Product;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\AttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;

/**
 * Class OrderItem
 *
 *
 * @ORM\Entity
 * @ORM\Table(name="sylius_order_item")
 * @ORM\AssociationOverrides({
 *      @ORM\AssociationOverride(
 *          name="variant",
 *          joinColumns=@ORM\JoinColumn(
 *              name="variant", referencedColumnName="id", nullable=true
 *          )
 *      )
 * })
 */
class OrderItem extends \Sylius\Component\Core\Model\OrderItem
{

    /**
     * @var
     * @ORM\ManyToOne(targetEntity="AppBundle\Model\Base\Entity\Product")
     */
    protected $product;

    /**
     * @return mixed
     */
    public function getProduct()
    {
        return $this->product;
    }

    /**
     * @param mixed $product
     */
    public function setProduct($product)
    {
        $this->product = $product;
    }
}

我能够覆盖“变体”列的定义并将该列设置为空,但我不知道如何更改targetEntity

【问题讨论】:

    标签: php doctrine-orm sylius


    【解决方案1】:

    如文档中所述,您无法更改关联的类型: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#association-override

    但是,您可以将 targetEntity 定义为接口(这似乎是默认的 sylius conf),

    targetEntity="AppBundle\Entity\ProductInterface"
    

    扩展你的接口文件中的原始文件

    namespace AppBundle\Entity;    
    use Sylius\Component\Core\Model\ProductInterface as BaseProductInterface;
    interface ProductInterface extends BaseProductInterface {}
    

    并在您的配置中添加映​​射

    doctrine:
        orm:
            resolve_target_entities:
                AppBundle\Entity\ProductInterface: AppBundle\Entity\Product
    

    这里有描述:http://symfony.com/doc/current/doctrine/resolve_target_entity.html

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      我没有找到在使用注解时覆盖关联的 targetEntity 值的方法,但在使用 PHP 映射(php 或 Symfony 中的 staticphp)时这是可能的。

      https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/php-mapping.html

      我们可以创建一个函数:

      function updateAssociationTargetEntity(ClassMetadata $metadata, $association, $targetEntity)
      {
          if (!isset($metadata->associationMappings[$association])) {
              throw new \LogicException("Association $association not defined on $metadata->name");
          }
          $metadata->associationMappings[$association]['targetEntity'] = $targetEntity;
      }
      

      并像这样使用它:

      updateAssociationTargetEntity($metadata, 'product', AppBundle\Model\Base\Entity\Product::class);
      

      这样,当 Doctrine 加载 AppBundle\Model\Order\Entity\OrderItem 类的元数据时,它首先加载父 (\Sylius\Component\Core\Model\OrderItem) 元数据,然后加载子类的 PHP 映射,我们在其中覆盖设置的关联映射加载父类元数据时。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        相关资源
        最近更新 更多