tags:

views:

67

answers:

1

Using cakephp: I am trying to update customer information and the address the customer is linked to. such that Customer.address_id = Address.id, and

Customer Model

$belongsTo = 'Address';

From the customers_controller

function profile($id = null)
{
  if (empty($this->data['Customer']))
  { 
    $this->Customer->id = $id;
    $this->data = $this->Customer->read();
  }
  else
  {
    $this->Customer->id = $this->data['Customer']['id'];
    $this->Customer->read();
    $this->Customer->save($this->data['Customer']);
    $this->Customer->Address->save($this->data['Address']);
  }
}

Customer correctly updates, but Address always inserts a new row. How do I get this address to update?

+1  A: 

first of all, take away lines 11 and 12. those serve no purpose. make sure your view contains form elements for Customer.id and Address.id. If you are just updating the Address you dont need line 13 either. The short answer is that Cakephp will insert row instead of update if the primary key is missing. In your case this means [Address][id].

Alexander Morland
I tried to assign the address id as $this->Customer->Address->id, apparently that didn't work.
Jack B Nimble
it needs to be in the data array
Alexander Morland