同一模型中的儿童 - CakePHP

发布于 2024-08-24 00:54:40 字数 178 浏览 1 评论 0原文

我的表“帖子”有字段:id、名称、内容、post_id 当我创建新帖子时,字段“post_id”=0,但是当我“回答”帖子时,字段“post_id”从已回答的帖子 ID 中获取 id。 我的问题是,如何通过 slug 字段宽度 post_id=0 (父级)获取帖子,并在一个查询中使用 'post_id' =parent.id 获取他的孩子?

My table 'Post' has field: id, name, content, post_id
When i create new post, the field 'post_id'=0, but when I "answer" to Post, the field 'post_id' get id off answered post ID.
My question is, how get post by slug field width post_id=0 (parent) and get his children with 'post_id' = parent.id in one query?

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

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

发布评论

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

评论(1

缱倦旧时光 2024-08-31 00:54:40

您可以在 Post 模型中建立关系。定义一个新模型(我们称之为“Answer”),让它扩展 Post 模型并在 Post 和 Answer 之间建立 hasMany 关系。然后,当您使用 $this->find 检索帖子时,答案也会自动检索。 (CakePHP 将为您完成编写查询的繁重工作)。

此链接可能会有所帮助:

http://book. cakephp.org/view/851/Multiple-relations-to-the-same-model

You could set up a relationship in the Post model. Define a new model (lets call this "Answer"), have it extend the Post model and set up a hasMany relationship between Post and Answer. Then, when you retrieve a Post with $this->find, the Answers will be automatically retrieved too. (CakePHP will do the grunt work of writing the queries for you).

This link might be helpful:

http://book.cakephp.org/view/851/Multiple-relations-to-the-same-model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文