我正在测试cakePHP中的"拥有和属于许多关系"。这是我的数据库模型:
接触有和属于许多接触
联系人有一个人
因此,我的联系模型如下所示:
public $hasOne = array(
'Person'
);
public $hasAndBelongsToMany = array(
'ContactLinks' => array(
'className' => 'Contact',
'joinTable' => 'contacts_contacts',
'foreignKey' => 'contact_id_1',
'associationForeignKey' => 'contact_id_2',
)
);
为了避免使用深度递归值,我使用了可包含的巴哈维。这是我的发现:
$data = $this->Contact->find('first', array(
'conditions' => array(
'Contact.id' => 15
),
'contains' => array(
'Contact' => array(
'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没有在联系人和人之间建立联系。
关于如何解决这个问题的任何想法?
谢谢!
我认为使用错误的别名时会出现问题。试试这个
$data = $this->Contact->find('first', array(
'conditions' => array(
'Contact.id' => 15
),
'contains' => array(
'ContactLinks' => array('Person'))
));
编辑:错别字警报 ->"包含"应该是"包含"
$data = $this->Contact->find('first', array(
'conditions' => array(
'Contact.id' => 15
),
'contain' => array(
'ContactLinks' => array('Person'))
));