【问题标题】:Doctrine override field associations?教义覆盖领域关联?
【发布时间】:2018-10-19 15:07:59
【问题描述】:

我正在关注这个:

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/override-field-association-mappings-in-subclasses.html#override-field-association-mappings-in-subclasses

这是我的代码:

<?php

namespace App\Entity\Type;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="attr")
 */
class TypeAssociation 
{

    /**
     * @ORM\ManyToOne(
     *     targetEntity="App\Entity\Attr",
     *     inversedBy="associationValues",
     *     cascade={"persist"}
     * )
     * @ORM\JoinColumn(name="attr_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
     */
    protected $attr;

}

/**
 * @ORM\Entity
 * @ORM\Table(name="attr_super")
 *
 * @ORM\AssociationOverrides({
 *      @ORM\AssociationOverride(name="attr",
 *          joinColumns=@ORM\JoinColumn(
 *              name="attr_id2", referencedColumnName="id", nullable=false
 *          )
 *      )
 * })
 */
class TypeAssociationBridge extends TypeAssociation
{

}

它正在按预期创建第二个表,但未创建覆盖字段 - 我做错了什么或不了解此功能?

我正在使用 Doctrine ORM v2.6.2 - Symfony 4.1.6

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    TypeAssociation 类的映射有误,因为它不应该是实体。用@ORM\MappedSuperclass代替@ORM\Entity就可以了:

    类型关联

    /**
     * @ORM\MappedSuperclass
     */
    class TypeAssociation
    {
        /**
         * @ORM\ManyToOne(targetEntity="App\Entity\Attr")
         * @ORM\JoinColumn(name="attr_id", referencedColumnName="id")
         */
        protected $attr;
    }
    

    TypeAssociationBridge

    /**
     * @ORM\Entity
     * @ORM\Table(name="attr_super")
     *
     * @ORM\AssociationOverrides({
     *      @ORM\AssociationOverride(name="attr",
     *          joinColumns=@ORM\JoinColumn(
     *              name="attr_id2", referencedColumnName="id"
     *          )
     *      )
     * })
     */
    class TypeAssociationBridge extends TypeAssociation
    {
    
    }
    

    参考:

    Association Override

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      相关资源
      最近更新 更多