Tolga
Tolga

Reputation: 282

Cakephp habtm relation updating instead of saving

Hello my problem is i try to save a new relation between a shop and a payment method the relation is habtm... shop and payment already exist. i want to ad more payment methods. But always when i save ,the old payment realtion in the shop_payment table is only updated, not a second one saved.... i read a lot i set unique to false but nothing changes that. Anyone got an idea?

Model

class Payment extends AppModel {
    var $hasAndBelongsToMany = array(
        'Mainshop'=>array('className'=>'Mainshop', 'unique'=>'false')
    );     
}

View

echo $this->Form->create('Mainshop');
echo $this->Form->input('name',array('default'=>$mainshop['Mainshop']['name']));

echo $this->Form->input('Payment.id', array(
'type' => 'select', 

'options' => array($payments),

    ));

 echo $this->Form->input('id', array('type'=>'hidden','value'=>$mainshop['Mainshop']            ['id'])); 
echo $this->Form->end('Edit Shop');?>

Controller

if (!empty($this->data)){

    $this->Mainshop->save($this->data);
    $this->redirect(array('action' => 'edit',$this->data['Mainshop']['id']));
}

Upvotes: 0

Views: 412

Answers (1)

del_dan
del_dan

Reputation: 873

My recommendation defines the relationship with all fields in model:

       var $hasAndBelongsToMany = array(
            'Mainshop'=>array(
               'className'=>'Mainshop', 
               'unique'=>'false',
               'joinTable' => 'shop_payments',
               'foreignKey' => 'payments_id',
               'associationForeignKey' => 'shop_id'
               )
        );

In the controller add create():

if (!empty($this->data)){
    $this->Mainshop->create();
    $this->Mainshop->save($this->data);
    $this->redirect(array('action' => 'edit',$this->data['Mainshop']['id']));
}

Upvotes: 1

Related Questions