2012-10-31 41 views
0

保存我有問題與HABTM + cakephp。HABTM只是主要模型與saveall()

在我的應用程序我有這個模型(與關係的定義):

Cliente:

public $hasAndBelongsToMany = array(
    'TiposComercio' => array(
     'className'    => 'TiposComercio', 
     'joinTable'    => 'tipos_comercio_cliente', 
     'foreignKey'   => 'cliente', 
     'associationForeignKey' => 'tipo_comercio' 
    ) 
); 

TiposComercio:

public $hasAndBelongsToMany = array(

    'Cliente' => array(
     'className' => 'Cliente', 
     'joinTable' => 'tipos_comercio_cliente', 
     'foreignKey' => 'tipo_comercio', 
     'associationForeignKey' => 'cliente' 
    ) 
); 

當表單被提交,我有數組$this->request->data這個:

array(

    'Cliente' => array(
     'password' => '*****', 
     'razao_social' => 'Teste', 
     'responsavel' => 'responsavel', 
     'cidade' => '1', 
     'cep' => '13560201', 
     'logradouro' => 'Log test', 
     'numero' => '', 
     'bairro' => '', 
     'complemento' => '', 
     'atividades' => '', 
     'username' => 'username11', 
     'TiposComercio' => array(
      (int) 0 => '1', 
      (int) 1 => '4' 
     ) 
    ) 

) 

當我執行$ this-> Cliente-> saveAll($ this-> request-> data);在ClientesController中我有以下問題:

  • 只保存客戶數據。 TiposComercio沒有保存。

怎麼了?

謝謝!

回答

2

你的數據數組的結構似乎是錯誤的。看一看手動保存HABTM數據:http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm

基礎上的例子,你的數據數組應該是這樣形成:

array(
    'Cliente' => array(
     'password' => '*****', 
     'razao_social' => 'Teste', 
     'responsavel' => 'responsavel', 
     'cidade' => '1', 
     'cep' => '13560201', 
     'logradouro' => 'Log test', 
     'numero' => '', 
     'bairro' => '', 
     'complemento' => '', 
     'atividades' => '', 
     'username' => 'username11', 
    ), 
    'TiposComecio' => array(
     (int) 0 => '1', 
     (int) 1 => '4' 
    ) 
) 
+0

非常感謝!你告訴我糾正錯誤的方法! – matheusvmbruno