【问题标题】:HABTM Just main model is saved with saveall()HABTM 只是主模型用 saveall() 保存
【发布时间】:2012-10-31 14:03:21
【问题描述】:

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

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

客户:

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 的任何内容都没有保存。

怎么了?

谢谢!

【问题讨论】:

    标签: cakephp cakephp-2.0 has-and-belongs-to-many


    【解决方案1】:

    您的数据数组的结构似乎有误。查看保存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'
        )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多