如何在cakephp中实现自引用(parent

编程入门 行业动态 更新时间:2024-10-07 14:31:22
本文介绍了如何在cakephp中实现自引用(parent_id)模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为categories的表。表格包含类别及其子(子类别)类别...

I have a table called categories. The table holds categories and their sub(subsub)categories...

这是一个简单的表格:

  • id
  • parent_id
  • title

这样我可以做出无限深度的类别...我希望cakephp会理解parent_id(我也尝试过category_id,但这使得cakePHP连接本身:D)

This way I can make categories with endless depth... I kinda hoped that cakephp would understand parent_id (I also tried category_id, but that makes cakePHP join on itself :D )

要解决这个问题的正确方法是什么?

What is the right way to tackle this?

注意:还有一个名为places的many to many表。这些地方可以属于一个或多个类别。

NOTE: There is also a 'many to many' table called places. Those places can belong to one or more categories.

推荐答案

你只需要像这样设置你的模型:

Tree behaviour is overkill for this situation. You just need to set your model up like this:

class Category extends AppModel { public $hasMany = array( 'Children'=>array( 'className'=>'Category', 'foreignKey'=>'parent_id' ) ); public $belongsTo = array( 'Parent'=>array( 'className'=>'Category', 'foreignKey'=>'parent_id' ) ); }

现在,当你在Category上做一个find你会得到两个额外的模型称为父(指向父ID)和儿童(它列出它的孩子)。

Now, when you do a find() on Category, you'll get two additional Models called Parent (which points to the parent id) and Children (which lists it's children).

更多推荐

如何在cakephp中实现自引用(parent

本文发布于:2023-10-23 02:29:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   cakephp   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!