[CakePHP 2.x]単一カラムを更新するModel::saveField()を試す


## 試み

* 比較用にModel::save()と、単一カラムを更新するModel::saveField()を試す。

## 事前準備

### テーブルを用意

```sql
CREATE TABLE comments(
    id SERIAL NOT NULL PRIMARY KEY,
    comment TEXT NOT NULL,
    created TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
```

### データを用意

```sql
INSERT INTO comments(comment) VALUES
('Comment 1'),
('Comment 2');
```

### モデルを用意

`app/Model/Comment.php` ※bakeで作る

### ビューを用意

`app/View/Comments/example.ctp`

```php
<pre>
<?php print_r($before); ?>
</pre>
<pre>
<?php print_r($after); ?>
</pre>
```

### コントローラを用意

`app/Controller/CommentsController.php` ※bakeで作る

## Model::save()で試す

* http://book.cakephp.org/2.0/ja/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

* `app/Controller/CommentsController.php`にsave()を加えて試す。

```php
class CommentsController extends AppController {
    public function save() {
        $comment = __FUNCTION__;

        $this->Comment->id = 1;

        $this->set('before', $this->Comment->read());

        $this->Comment->save(array(
            'Comment' => array(
                'comment' => $comment,
            ),
        ));

        $this->set('after', $this->Comment->read());

        $this->render('example');
    }
}
```

* 実行結果

```
Array
(
    [Comment] => Array
        (
            [id] => 1
            [comment] => Comment 1
            [created] => 2015-02-22 10:23:34.10898
            [modified] => 2015-02-22 10:23:34.10898
        )

)
Array
(
    [Comment] => Array
        (
            [id] => 1
            [comment] => save
            [created] => 2015-02-22 10:23:34.10898
            [modified] => 2015-02-22 10:23:34.10898
        )

)
```

```sql
UPDATE "public"."comments"
SET "id" = 1, "comment" = 'save', "created" = '2015-02-22 10:23:34.10898', "modified" = '2015-02-22 10:23:34.10898'
WHERE "public"."comments"."id" = 1
```

* save()だとread()で読み込んだデータも更新に含まれる。
* 使い方によっては$fieldListも合わせて使う。

## Model::saveField()で試す

* http://book.cakephp.org/2.0/ja/models/saving-your-data.html#model-savefield-string-fieldname-string-fieldvalue-validate-false

* `app/Controller/CommentsController.php`にsaveField()を加えて試す。

```php
class CommentsController extends AppController {
    public function saveField() {
        $comment = __FUNCTION__;

        $this->Comment->id = 2;

        $this->set('before', $this->Comment->read());

        $this->Comment->saveField(
            'comment',
            $comment
        );

        $this->set('after', $this->Comment->read());

        $this->render('example');
    }
}
```

* 実行結果

```
Array
(
    [Comment] => Array
        (
            [id] => 2
            [comment] => Comment 2
            [created] => 2015-02-22 10:23:34.10898
            [modified] => 2015-02-22 10:23:34.10898
        )

)
Array
(
    [Comment] => Array
        (
            [id] => 2
            [comment] => saveField
            [created] => 2015-02-22 10:23:34.10898
            [modified] => 2015-02-22 02:25:54
        )

)
```

```sql
UPDATE "public"."comments"
SET "comment" = 'saveField', "modified" = '2015-02-22 02:25:54'
WHERE "public"."comments"."id" = 2
```

* saveField()だと対象とするcommentカラムとmodifiedカラムが更新される。