【发布时间】:2014-02-26 22:44:52
【问题描述】:
我在使用 cakephp 的 habtm 时遇到问题,我还不知道如何解决。我有三个表:setores、veiculos 和 setores_veiculos。
Setor 模型:
public $hasAndBelongsToMany = array(
'Veiculo' => array(
'className' => 'Veiculo',
'joinTable' => 'setores_veiculos',
'foreignKey' => 'setor_id',
'associationForeignKey' => 'veiculo_id',
'unique' => 'keepExisting'
)
);
Veiculo 模型:
public $hasAndBelongsToMany = array(
'Setor' => array(
'className' => 'Setor',
'joinTable' => 'setores_veiculos',
'foreignKey' => 'veiculo_id',
'associationForeignKey' => 'setor_id',
'unique' => 'keepExisting'
)
);
在我的 setores 控制器中,我有:
$options = array('conditions' => array('Setor.' . $this->Setor->primaryKey => $id));
$setores = $this->Setor->find('first', $options);
然后在我的调试中:
array(
'Setor' => array(
'id' => (int) 5,
'nome' => '2º Batalhão',
'secretaria_id' => (int) 6,
'sigla' => '2º BPM',
'status_id' => (int) 1
),
'Veiculo' => array()
)
我不明白为什么没有列出 Veiculo(车辆)。插入工作正常。
我预计:
'Veiculo' => array(
0 => array(
'id' => 1,
'name' => 'Corolla'
),
1 => array(
'id' => 2,
'name' => 'Hillux'
)
);
在我的 veiculos 控制器中列出了 setores。有什么建议吗?
提前谢谢你,对不起我的英语。
【问题讨论】:
标签: php cakephp-2.0 has-and-belongs-to-many