我正在 cakePHP 中测试 Has And Belongs To Many 关系。这是我的数据库模型:
联系人拥有并属于多个联系人
联系人有一个人
因此,我的联系人模型如下所示:
public $hasOne = array(
'Person'
);
public $hasAndBelongsToMany = array(
'ContactLinks' => array(
'className' => 'Contact',
'joinTable' => 'contacts_contacts',
'foreignKey' => 'contact_id_1',
'associationForeignKey' => 'contact_id_2',
)
);
为了避免使用深度递归值,我使用了可包含的bahavior。这是我的发现:
$data = $this->Contact->find('first', array(
'conditions' => array(
'Contact.id' => 15
),
'contains' => array(
'Contact' => array(
'Person'
),
),
));
这个请求对我来说很好。由于 Contact hasOne Person,我在 Contact 容器中查找 Person。但是,它不起作用:(
我的结果数组如下所示:
[ContactLinks] => Array
(
[0] => Array
(
[id] => 56
[created] => 0000-00-00 00:00:00
[modified] => 2013-10-31 11:41:28
[creator_id] => 1
[modificator_id] => 2
[type] => person
[ContactsContact] => Array
(
[id] => 1
[contact_id_1] => 15
[contact_id_2] => 56
[link_type] => firm
[link_description] => Stagiaire
)
)
[1] => Array
(
[id] => 30
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
[creator_id] => 1
[modificator_id] =>
[type] => person
[ContactsContact] => Array
(
[id] => 2
[contact_id_1] => 15
[contact_id_2] => 30
[link_type] =>
[link_description] => Patron
)
)
)
似乎 CakePHP 没有在 Contact 和 Person 之间建立联系。
关于如何解决这个问题的任何想法?
谢谢 !