同一个表上的多对一关系子页面/页面?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题的答案就在这里,在 Doctrine 文档中的“一对多,自引用”下:
https://www.doctrine-project.org/projects/doctrine-orm /en/latest/reference/association-mapping.html#one-to-many-self-referencing
这是一个示例:
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: