15. 5th. Save new model
準備はできたのでモデルを保存してみましょう
$wiki_page = Model_Wiki_Page::forge();
$wiki_page->title = 'version 1';
$wiki_page->body = 'VERSION 1';
$wiki_page->save();
+----+-----------+-----------+---------------------+---------------------+
| id | title | body | temporal_start | temporal_end |
+----+-----------+-----------+---------------------+---------------------+
| 1 | version 1 | VERSION 1 | 2013-03-17 10:15:14 | 2038-01-18 22:14:08 |
+----+-----------+-----------+---------------------+---------------------+
16. 6th. Update model
次にID:1のモデルをアップデートしてみます
$wiki_page = Model_Wiki_Page::find(1);
$wiki_page->title = 'version 2';
$wiki_page->body = 'VERSION 2';
$wiki_page->save();
レコードの更新ではなく、新しく追加されました!
+----+-----------+-----------+---------------------+---------------------+
| id | title | body | temporal_start | temporal_end |
+----+-----------+-----------+---------------------+---------------------+
| 1 | version 1 | VERSION 1 | 2013-03-17 10:15:14 | 2013-03-17 10:17:27 |
| 1 | version 2 | VERSION 2 | 2013-03-17 10:17:27 | 2038-01-18 22:14:08 |
+----+-----------+-----------+---------------------+---------------------+
17. 7th. Get revisions
何回か更新した後にリビジョンを取得してみます。
$revisions = Model_Wiki_Page::find_revisions_between(1);
foreach($revisions as $rev){
echo $rev->title . ' : '. $rev->temporal_start . ' - '
. $rev->temporal_end . PHP_EOL;
}
ID:1に複数のバージョンが保持されてる事がわかります
version 1 : 2013-03-17 10:15:14 - 2013-03-17 10:17:27
version 2 : 2013-03-17 10:17:27 - 2013-03-17 10:21:57
version 3 : 2013-03-17 10:21:57 - 2013-03-17 10:23:20
version 4 : 2013-03-17 10:23:20 - 2013-03-17 10:23:28
version 5 : 2013-03-17 10:23:28 - 2038-01-18 22:14:08
18. 8th. Overwrite
バージョンを作らずに更新する事もできます
$wiki_page = Model_Wiki_Page::find(1);
$wiki_page->title = 'updated';
$wiki_page->overwrite();
+----+-----------+-----------+ +----+-----------+-----------+
| id | title | body | | id | title | body |
+----+-----------+-----------+ +----+-----------+-----------+
| 1 | version 1 | VERSION 1 | | 1 | version 1 | VERSION 1 |
| 1 | version 2 | VERSION 2 | | 1 | version 2 | VERSION 2 |
| 1 | version 3 | VERSION 3 | | 1 | version 3 | VERSION 3 |
| 1 | version 4 | VERSION 4 | | 1 | version 4 | VERSION 4 |
| 1 | version 5 | VERSION 5 | | 1 | updated | VERSION 5 |
+----+-----------+-----------+ +----+-----------+-----------+
19. 9th. Delete model
削除してみましょう!
$wiki_page = Model_Wiki_Page::find(1);
$wiki_page->delete();
+----+---------+-----------+---------------------+---------------------+
| id | title | body | temporal_start | temporal_end |
+----+---------+-----------+---------------------+---------------------+
---( 略 )---
+----+---------+-----------+---------------------+---------------------+
| 1 | updated | VERSION 5 | 2013-03-17 10:23:28 | 2038-01-18 22:14:08 |
+----+---------+-----------+---------------------+---------------------+
+----+---------+-----------+---------------------+---------------------+
| id | title | body | temporal_start | temporal_end |
+----+---------+-----------+---------------------+---------------------+
---( 略 )---
+----+---------+-----------+---------------------+---------------------+
| 1 | updated | VERSION 5 | 2013-03-17 10:23:28 | 2013-03-17 10:35:17 |
+----+---------+-----------+---------------------+---------------------+