从MappedSuperClass到实体的学说关系

时间:2020-10-13 12:32:23

标签: symfony doctrine

我最终希望有一个抽象基类,该基类为所有继承的Entity-class定义与另一个实体的关系。还有使用symfony的 make:entity -命令创建的测试设置。 我唯一更改的是将MappedSuperClass的属性可见性从私有更改为受保护,以使扩展程序可以看到它们。

这是MappedSuperClass:

[...]

/**
 * Class TestMappedSuperclass
 *
 * @package App\Entity
 * @ORM\MappedSuperclass()
 */
class TestMappedSuperclass
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity=TestTarget::class, mappedBy="testMappedSuperclass")
     */
    protected $targets;

    public function __construct()
    {
        $this->targets = new ArrayCollection();
    }

    //..getters/setters,add/remove...
} 

这是我映射的目标实体:

[...]    
/**
 * @ORM\Entity(repositoryClass=TestTargetRepository::class)
 */
class TestTarget
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=TestMappedSuperclass::class, inversedBy="targets")
     */
    private $testMappedSuperclass;

    // ..getters,setters..
}

这是扩展实体类:

[...]
/**
 * @ORM\Entity(repositoryClass=TestEntityRepository::class)
 */
class TestEntity extends TestMappedSuperclass
{
}

因此,当我尝试执行 make:migration 时,会产生以下错误:

Column name `id` referenced for relation from App\Entity\TestTarget towards 
App\Entity\TestMappedSuperclass does not exist.

使用:Symfony 5.1,Doctrine 2.7

1 个答案:

答案 0 :(得分:0)

AFAIK,您需要具有在TestEntity中受保护的重复ID:

/**
 * @ORM\Entity(repositoryClass=TestEntityRepository::class)
 */
class TestEntity extends TestMappedSuperclass
{
    /**
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

我从一个我使用像这样的mappingSuperClass的项目中获得了这个,虽然我没有关联其中的实体

相关问题