在使用 MVC 框架时,强烈建议遵循 its conventions。因此,一些更改可能对您有益。
您正在寻找它的“用户”表和“能力”表之间的 HABTM(拥有并属于许多)关联 *。我猜,根据您的表格设计,一个用户可以有多种能力,否则您应该检查 hasMany 关联。
应该是这样的:
表功能:
从能力中选择*;
+----+------------+----------------------+----------+
| id | category | subcategoy | created | modified |
+----+------------+------------+---------+----------+
| 1 | Programmer | ASP | | |
| 2 | Programmer | PHP | | |
| 3 | Musician | Classical | | |
+----+------------+------------+---------+----------+
表用户:
从用户中选择 *;
+----+-------+---------------------+---------------------+
| id | name | created | modified | **
+----+-------+---------------------+---------------------+
| 1 | Roy | 2012-08-15 02:52:18 | 2013-01-17 03:25:28 |
| 2 | Ben | 2012-11-10 03:36:12 | 2012-11-10 03:36:12 |
+----+-------+---------------------+---------------------+
HABTM 的关系表:
从 habilities_users 中选择 *;
+----+-------------+-------------------+----------+
| id | hability_id | user_id | created | modified |
+----+-------------+---------+---------+----------+
| 1 | 1 | 2 | | |
| 2 | 2 | 1 | | |
+----+-------------+---------+---------+----------+
查看 habilities_users 中的引用列,它们是带有 _id 后缀的单数列,可以与 CakePHP 一起使用。
定义模型类也很重要,因为您可以在其中定义它们的所有关联。
app/Model/User.php
<?php
class User extends AppModel {
public $hasAndBelongsToMany = array('Hability');
}
app/Model/Hability.php
<?php
class Hability extends AppModel {
public $hasAndBelongsToMany = array('User');
}
表 habilities_users 不需要模型文件,其行为和属性隐含在其关联模型的声明中。
* 使用这些名称也是 CakePHP 的方式。 [link]
** 在每个表中添加“created”和“modified”将自动为每条记录存储这些事件。