0

我在使用 HABTM + cakephp 时遇到问题。

在我的应用程序中,我有这个模型(带有关系定义):

客户:

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

提示:

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 没有任何东西被保存。

怎么了?

谢谢!

4

1 回答 1

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'
    )
)
于 2012-10-31T14:12:00.600 回答