LoginSignup
2
2

More than 5 years have passed since last update.

CakePHP、データベースをupdateする時の注意点

Posted at

CakePHPで、データベースのupdateをかけるフィールドを特定のカラムに限定して、
さらにバリデーションを無視する場合の書き方。

注)以下のPostモデルの項目commentにはバリデーションでnotEmptyが設定されている。

$data = array('id' => $myId, 'comment' => NULL);
$this->Post->save($data, false, array('comment'));

save()の引数の真ん中の false を true にすると、notEmpty が有効なため、commentに NULL を入れようとしても全く入らない(update文が実行されない)。

3つ目の引数の array() が updateするカラムの指定で、これを入れないとデータベース上でdefault valueが設定されているようなカラムも一緒に更新しようとする。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2