同一模型中的儿童 - CakePHP
我的表“帖子”有字段: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 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