drg17404 2009-07-10 02:59
浏览 76
已采纳

Cakephp模型类中的save方法会选择创建和更新记录吗?

Basically, I want to achieve the same thing as ON DUPLICATE KEY in MySQL.

Here's a contrived example:

$rec = array('Foo' => array(
  'id' => 999,  // Assume there isn't already a record with this id
  'website' => 'google'
));
$this->Foo->save($rec);

// save with different 'website' value
$rec['Foo']['website'] = 'stackoverflow';
$this->Foo->save($rec);

Does the last line update the record that was created just a few lines up?

  • 写回答

1条回答 默认 最新

  • dongzipu7517 2009-07-10 03:03
    关注

    If you call save(), supplying a record with an existing id (or whatever is the primary key), it will update, otherwise it will create a new record.

    So in your case, yes, it would update the record that was first created at the top.

    Update

    Here's the supporting documentation:

    Creating or updating is controlled by the model's id field. If $Model->id is set, the record with this primary key is updated. Otherwise a new record is created.

    Under the documentation for Model::save().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?