同一个表上的多对一关系子页面/页面?

发布于 2024-10-21 22:26:49 字数 455 浏览 2 评论 0原文

使用 ZF 和 Doctrine。我有一个表“页面”。我想要拥有它,以便某些页面可以有与其关联的子页面。子页面还应该引用其(唯一的)父页面。

我知道我可以将其拆分为两个实体(页面/子页面),但我知道一个实体是可能的,但无法弄清楚如何使这种关系发挥作用。

我设想的方式是,第二个表将是映射表(page_id,parent_page_id)。

我在我的实体中使用注释引用,这就是我到目前为止正在做的事情,在深夜的紧急情况下,任何帮助将不胜感激。

    /**
 * @OneToMany(targetEntity="Page", mappedBy="parentPage")
 */
private $subPages;


/**
 * @ManyToOne(targetEntity="Page", inversedBy="subPages")
 */
private $parentPage;

Using ZF and Doctrine. I have a table 'pages'. I want to have it so that some pages can have sub pages associated with it. The sub pages should also have a reference to their (one and only) parent page.

I know I could split this between 2 entities (page / subpage), but I know it's possible with one entity, but can't figure out how to make the relationship work.

The way I invision it, is a second table would be the mapping table (page_id, parent_page_id).

I'm using annotation reference in my entities and here is what i'm doing so far, any help would be appreciated, in a late night crunch.

    /**
 * @OneToMany(targetEntity="Page", mappedBy="parentPage")
 */
private $subPages;


/**
 * @ManyToOne(targetEntity="Page", inversedBy="subPages")
 */
private $parentPage;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2024-10-28 22:26:49

这个问题的答案就在这里,在 Doctrine 文档中的“一对多,自引用”下:

https://www.doctrine-project.org/projects/doctrine-orm /en/latest/reference/association-mapping.html#one-to-many-self-referencing

这是一个示例:

class Pages{
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /**
     * one page have several referred pages
     * @var Collection
     * @ORM\OneToMany(targetEntity="Pages", mappedBy="parentPage")
     */
    private $subPages;

    /**
     * many referred pages have one referral page
     * @ORM\ManyToOne(targetEntity="Pages", inversedBy="subPages")
     * @ORM\JoinColumn(name="parent_page", referencedColumnName="id")
     */
    private $parentPage;
    private $referredBy;



    public function __construct()
    {
        $this->subPages = new Doctrine\Common\Collections\ArrayCollection();
    }
}

The answer for this question is here, in the Doctrine documentation, under One-To-Many, Self-referencing:

https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-self-referencing

Here is an example:

class Pages{
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /**
     * one page have several referred pages
     * @var Collection
     * @ORM\OneToMany(targetEntity="Pages", mappedBy="parentPage")
     */
    private $subPages;

    /**
     * many referred pages have one referral page
     * @ORM\ManyToOne(targetEntity="Pages", inversedBy="subPages")
     * @ORM\JoinColumn(name="parent_page", referencedColumnName="id")
     */
    private $parentPage;
    private $referredBy;



    public function __construct()
    {
        $this->subPages = new Doctrine\Common\Collections\ArrayCollection();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文