在使用MVC框架時,強烈建議遵循its conventions。所以一些改變可能對你有益。
您正在尋找其「用戶」表和「性能」表之間的HABTM(具有和屬於許多)關聯。我猜測,通過你的表的設計,用戶可以有多個habilities,否則你應該檢查hasMany關聯。
應該是這樣的:
表habilities: SELECT * FROM habilities;
+----+------------+----------------------+----------+
| id | category | subcategoy | created | modified |
+----+------------+------------+---------+----------+
| 1 | Programmer | ASP | | |
| 2 | Programmer | PHP | | |
| 3 | Musician | Classical | | |
+----+------------+------------+---------+----------+
表用戶: select * from users;
+----+-------+---------------------+---------------------+
| 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關係表: SELECT * FROM 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');
}
應用/型號/ Hability.php
<?php
class Hability extends AppModel {
public $hasAndBelongsToMany = array('User');
}
表habilities_users不需要模型文件,它的行爲和屬性在其關聯的模型的聲明暗示。
*使用這些名稱也是CakePHP的方式。 [link]
**在每個表格中添加「創建」和「修改」將自動存儲每條記錄的這些事件。
嗨@Alexandre,請你可以發佈你的模型代碼,以便我們可以看到你當前的關聯? – 2013-05-04 20:43:43