2014-02-26 36 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的數據轉儲。