在cakephp中保存HABTM关联数据

时间:2016-10-13 09:45:33

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

我有点问题。我的商品模型已经拥有并且属于许多与用户模型的关联。在数据库中,它们通过commodities_users表相关联。因此,我希望Cakephp在创建新商品时在DB的commodities_users表中创建新记录。但它不起作用。 我的$this->request->data数组(当我想保存一个新商品时)看起来像这样

array(
    'Commodity' => array(
        'commodity_type' => '0',
        'commoditygroup_id' => '',
        'name' => 'asdfad',
        'code' => '',
        'ean' => '',
        'costprice' => '',
        'saleprice' => '12512,123',
        'default_vatrate' => '',
        'saleprice_gross' => '',
        'sync_cashconnector' => '1',
        'commoditysetting_id' => '',
        'default_amount_enabled' => '0',
        'default_amount' => '',
        'stock_min' => '',
        'stock' => '',
        'comment' => ''
    ),
    'User' => array(
        (int) 0 => array(
            'id' => '23'
        ),
        (int) 1 => array(
            'id' => '24'
        ),
        (int) 2 => array(
            'id' => '30'
        ),
        (int) 3 => array(
            'id' => '31'
         )
));

我正在以这种方式保存商品模型$this->Commodity->saveAll($this->request->data);

我的cakePhp版本是2.4。 关系是

var $hasAndBelongsToMany = array(
            'User' => array(
                    'className' => 'User',
                    'joinTable' => 'commodities_users',
                    'foreignKey' => 'commodity_id',
                    'associationForeignKey' => 'user_id',
            ),
    );

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您没有将正确的数据输入saveAll()

根据CakePHP文档,构建数据的正确方法如下:

array(
    array(
        'Commodity' => array(
            'commodity_type' => '0',
            'commoditygroup_id' => '',
            'name' => 'asdfad',
            'code' => '',
            'ean' => '',
            'costprice' => '',
            'saleprice' => '12512,123',
            'default_vatrate' => '',
            'saleprice_gross' => '',
            'sync_cashconnector' => '1',
            'commoditysetting_id' => '',
            'default_amount_enabled' => '0',
            'default_amount' => '',
            'stock_min' => '',
            'stock' => '',
            'comment' => ''
        ),
        'User' => array(
            'User' => array('23','24','30','31')
        )
    )
);

然后使用以下命令保存数据:

$this->Commodity->saveAll($this->request->data, array('deep' => true)) 

有关进一步参考,请参阅

答案 1 :(得分:0)

好的,我自己解决了。我只是如此愚蠢,只是在商品模型中写了HABTM协会。为用户模型添加相同内容可解决问题。并且$this->reuqest->data数组看起来如此

array(
    'Commodity' => array(
        'commodity_type' => '0',
        'commoditygroup_id' => '',
        'name' => 'asdfad',
        'code' => '',
        'ean' => '',
        'costprice' => '',
        'saleprice' => '12512,123',
        'default_vatrate' => '',
        'saleprice_gross' => '',
        'sync_cashconnector' => '1',
        'commoditysetting_id' => '',
        'default_amount_enabled' => '0',
        'default_amount' => '',
        'stock_min' => '',
        'stock' => '',
        'comment' => ''
    ),
    'User' => array(
        'id' => array(
            '0' => '23',
            '1' => '24',
            '2' => '25'

));

谢谢你的时间!