0
votes

I have a little problem. My Commodity model has Has and belongs to many association with User model. In the DB they are related through commodities_users table. So, I want Cakephp to create new records in commodities_users table in DB when the new Commodity was created. But it doesn't work. My $this->request->data array (when I want saving one new commodity) looks like this

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'
         )
));

And I am saving the Commodity model this way $this->Commodity->saveAll($this->request->data);

My cakePhp version is 2.4. The relationship is

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

What am I doing wrong ?

2
You didn't mention your cakephp version, and also didn't display your relations on model code and table schema. if you share it will be more easier to help youtarikul05
@tarikul05, take a look, I did some changes.handkock
Will you please add "public" instate of "var" in your relation,tarikul05
@tarikul05, with other Models it works, so i do not think that is the problemhandkock
@ВладиславВадимович are you able to save data in commodities table?Faiyaz Alam

2 Answers

0
votes

You are not feeding the right data into saveAll().

As per the CakePHP docs, the proper way of structuring your data is as follows:

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')
        )
    )
);

You then save the data with:

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

For further reference, see

0
votes

Ok, I solved it by myself. I was just so dumm and wrote the HABTM association just in Commodity Model. Adding the same for User Model fixed the problem. And the $this->reuqest->data array looked so

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'

));

Thanks for your time!