I normally save new data to the database like this:
$this->MyTable->set(array(
'id' => $id,
'code' => $temp_code,
'status' => $status,
'age' => $age,
'location' => $location,
'money' => $money
));
$this->MyTable->save();
If the ID already exists in the database I update its corresponding fields like this:
$this->Adtweet->id = $id;
$this->Adtweet->saveField('code', $temp_code);
$this->Adtweet->saveField('status', $status);
Is there a better or 'proper' way to do this?
When I attempt to enter an ID that already exists and I use the set
function, I get the following SQL integrity error:
(Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '150245925' for key 'PRIMARY')
How can I write a function that cleanly handles duplicate entries without returning an error?
"; } else { echo "$twitter_user_id not exists
"; $this->ModelName->create( array ('twitter_user_id' => $twitter_user_id)); } $data = array('ModelName' => array('age' => 15, 'last_satatus' => 'online' )); $this->ModelName->save($data); }` – trante Jun 20 '12 at 21:36