2011-03-27 103 views
0

我有一個「友誼」表:CakePHP中的友誼模型

id | user_id_1 | user_id_2 |接受

基本上它引用了2個用戶(其中​​user_id_1是請求者)幷包含一個接受的標誌(是否user_id_2已接受請求)。

如何在CakePHP中設置模型,以便它自動附加用戶信息從我的用戶表中?

回答

3

使用在模型中屬於關聯關係: http://book.cakephp.org/view/1042/belongsTo

這可能看起來像:

class Friendship extends AppModel { 
    var $name = 'Friendship'; 
    var $belongsTo = array(
    'user_id_1' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id' 
    ), 
    'user_id_2' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id' 
    ) 
); 
}