猿问

如何在单个超映射类中创建多对一关系

我正在为我的无头symfony后端创建一个简单的CMS捆绑包,并且我正在尝试将Page映射到具有父子关系的页面(许多子级映射到一个父级),并且我有这个类映射的超类来创建可重用的代码,这是一个关于我试图存档的内容的缩小示例:



use Doctrine\ORM\Mapping as ORM;


/**

 * @ORM\MappedSuperclass()

 */

class Test

{

    /**

     * @ORM\Column(name="id", type="integer")

     * @ORM\Id

     * @ORM\GeneratedValue(strategy="AUTO")

     */

    protected $id;


    public function getId()

    {

        return $this->id;

    }


    /**

     * @ORM\ManyToOne(targetEntity="Ziebura\CMSBundle\Entity\Test")

     */

    protected $parent;


    public function getParent()

    {

        return $this->parent;

    }


    public function setParent($parent)

    {

        $this->parent = $parent;

    }

}

然后,我将此类扩展为普通实体以创建数据库表


<?php


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

use Ziebura\CMSBundle\Entity\Test as BaseTest;


/**

 * @ORM\Table(name="test")

 * @ORM\Entity(repositoryClass="App\Repository\TestRepository")

 */

class Test extends BaseTest

{

}

问题是我得到了这个教义例外


Column name `id` referenced for relation from App\Entity\Test towards Ziebura\CMSBundle\Entity\Test does not exist. 

我不太明白为什么它会产生这个错误,或者我试图存档的事情是不可能的,我已经在映射的超类上做了关系,但它是2个或更多的表,而不仅仅是一个。我已经尝试过创建$children字段,但它不起作用,仍然产生上述错误。有没有人试图创造一些类似的东西?我在教义文档中找不到任何关于此的内容,只能找到如何映射2个不同的超类。我想最简单的方法是在App命名空间中而不是在捆绑包中指定关系,但如果我必须在每个项目中声明使用捆绑包,这几乎破坏了可重用代码的目的。我相信堆栈让我们弄清楚这一点。谢谢!


杨__羊羊
浏览 189回答 2
2回答

www说

让我们阅读有关此内容的教义文档:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#inheritance-mapping映射超类是一个抽象或具体的类,它为其子类提供持久实体状态和映射信息,但其本身不是实体。通常,此类映射超类的目的是定义多个实体类通用的状态和映射信息。...映射的超类不能是实体,它不能是可查询的,并且由映射的超类定义的持久关系必须是单向的(仅具有所属方)。这意味着在映射的超类上根本不可能进行一对多关联。据此:映射的超类不能是实体不能有一对多关系 - 所以如果你将ManyToOne定义为同一类,那么它也会在同一类上创建OneToMany - 正如你上面读到的,这是被禁止的。

呼唤远方

由于某种原因,仅更改BaseTest中的完整实体路径解决了引发异常的应用程序并起作用,如果有人会遇到相同的问题,请尝试更改&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ORM\ManyToOne(targetEntity="Ziebura\CMSBundle\Entity\Test")&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $parent;&nbsp; &nbsp; public function getParent()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->parent;&nbsp; &nbsp; }&nbsp; &nbsp; public function setParent($parent)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->parent = $parent;&nbsp; &nbsp; }自&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ORM\ManyToOne(targetEntity="Test")&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $parent;&nbsp; &nbsp; public function getParent()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->parent;&nbsp; &nbsp; }&nbsp; &nbsp; public function setParent($parent)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->parent = $parent;&nbsp; &nbsp; }如果有人知道为什么它必须这样,我非常感谢对我的答案发表评论。
随时随地看视频慕课网APP
我要回答