我有一個名爲user_relationship的表。其中有兩個外鍵引用回用戶表來映射他們是朋友。
CREATE TABLE `user_relationships` (
`id` int(11) unsigned NOT NULL auto_increment,
`status` varchar(255) default 'pending',
`time` datetime default NULL,
`user_id` int(11) unsigned NOT NULL,
`friend_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_relationships_users1` (`user_id`),
KEY `fk_user_relationships_users2` (`friend_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
當我嘗試烤它自然不明白,friend_id必須引用用戶模塊。我想手動編輯的代碼。可是,有一些問題了解哪些部分在以下
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'friend_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
編輯在我想是指的friend_id到用戶表
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'friend_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
我嘗試了這部分代碼這樣做..我需要改變什麼嗎?
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
可以解釋爲什麼我們需要使用$ hasAndBelongsToMany。不能我用2 hasMany完成它? – 2010-09-27 07:46:37
http://book.cakephp.org/view/1046/Multiple-relations-to-the-same-model – 2010-09-27 07:51:43
@哈沙更新了一個嘗試的解釋... – deceze 2010-09-27 08:04:52