原则覆盖字段关联?

时间:2018-10-19 15:07:59

标签: symfony doctrine-orm

我正在关注:

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

1 个答案:

答案 0 :(得分:0)

您的TypeAssociation类映射错误,因为它不应该是Entity。在其上使用@ORM\MappedSuperclass而不是@ORM\Entity

TypeAssociation

/**
 * @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