Question

I am updating my Retailer Model which belongsTo Address Model, when I create new record it easily create new row for address and insert its key on Retailer Model:

View:

echo $this->Form->create('Retailer');
echo $this->Form->input('Name');
echo $this->Form->input('Address.add_line1');
echo $this->Form->input('Address.add_line2');
echo $this->Form->end('Submit');

Model (Retailer):

public $belongsTo = 'Address';

RetailersController->add()

$this->Retailer->create();
$this->Retailer->saveAll($this->request->data);

However When I tried to update Retailer the Retailer Model updated but insted of updating previous Address record it creates new record and puts its key on Retailer table:

RetailersController->edit($id = null)
$this->Retailer->id = $id;
$this->Retailer->saveAll($this->request->data);

I also tried this but not working :

RetailersController->edit($id = null)
$post = $this->Retailer->findById($id);
$this->Retailer->id = $id;
$this->request->data['Retailer']['address_id'] = $post['Retailer']['address_id'];
$this->Retailer->saveAll($this->request->data);
Was it helpful?

Solution

You probably just need to tell it the id of the address.

echo $this->Form->input('Address.id');

Otherwise it will perform an create, as you said.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top