2014-02-26 42 views
1

我有一个与cakephp habtm的问题,我不知道如何解决。我有三张表:setores,veiculos和setores_veiculos。CakePHP 2.x HABTM返回没有匹配的结果

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。任何建议?

非常感谢您对我的英语感谢。

回答

0

看起来很好。尝试改变

'unique' => 'keepExisting' 

'unique' => true 

目前,我用简单的规则去,因为,如果我用一个简单的链接表,即不存储任何数据文件中解释,我使用HABTM独特设置为true,否则我用的hasMany通过(在加入模型)组唯一keepExisting以免失去额外存储的数据。

发布setores_veiculos的数据转储。