このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
|
apricot:app:db-model [2020/05/11 22:12] y2sunlight |
apricot:app:db-model [2020/05/23 14:58] (現在) y2sunlight [モデルクラス] |
||
|---|---|---|---|
| 行 14: | 行 14: | ||
| * Apricot データベースとモデル | * Apricot データベースとモデル | ||
| * [[apricot: | * [[apricot: | ||
| - | * [[apricot: | + | * [[apricot: |
| * [[apricot: | * [[apricot: | ||
| * [[apricot: | * [[apricot: | ||
| - | * [[apricot: | + | * [[apricot: |
| \\ | \\ | ||
| 行 295: | 行 295: | ||
| ^メソッド名^機能^ | ^メソッド名^機能^ | ||
| - | |for_table()\\ :ORM|ORMオブジェクトの取得\\ テーブル名(snake_case)はクラス名(UpperCamelCase)から自動判定します。| | + | |tableName()\\ :string|テーブル名の取得\\ テーブル名(snake_case)はクラス名(UpperCamelCase)から自動判定します。| |
| + | |for_table()\\ : | ||
| |findAll()\\ : | |findAll()\\ : | ||
| |findOne\\ (int $id): | |findOne\\ (int $id): | ||
| |create\\ (array $inputs=null): | |create\\ (array $inputs=null): | ||
| - | |insert\\ (array $inputs):bool|レコードの挿入| | + | |insert\\ (array $inputs):ORM|レコードの挿入| |
| - | |update\\ ($id, array $inputs):bool|レコードの更新\\ レコードが存在しない時、ApplicationExceptionが発生します。\\ 楽観的ロック例外を検知した時、OptimissticLockExceptionが発生します。| | + | |update\\ ($id, array $inputs):ORM|レコードの更新\\ レコードが存在しない時、ApplicationExceptionが発生します。\\ 楽観的ロック例外を検知した時、OptimissticLockExceptionが発生します。| |
| - | |delete\\ ($id):bool|レコードの削除\\ レコードが存在しない時、ApplicationExceptionが発生します。| | + | |delete\\ ($id):ORM|レコードの削除\\ レコードが存在しない時、ApplicationExceptionが発生します。| |
| + | |isSuccess()\\ : | ||
| ソースコードを以下に示します。 | ソースコードを以下に示します。 | ||
| 行 319: | 行 321: | ||
| class Model | class Model | ||
| { | { | ||
| + | /** | ||
| + | * 最新の更新結果(insert/ | ||
| + | * @var bool | ||
| + | */ | ||
| + | private $success = false; | ||
| + | |||
| + | /** | ||
| + | * テーブル名の取得 | ||
| + | * @return string | ||
| + | */ | ||
| + | public function tableName(): | ||
| + | { | ||
| + | return snake_case(get_short_class_name($this)); | ||
| + | } | ||
| + | | ||
| /** | /** | ||
| * テーブルの取得 | * テーブルの取得 | ||
| 行 359: | 行 376: | ||
| * 新規保存 | * 新規保存 | ||
| * @param array $inputs | * @param array $inputs | ||
| - | * @return | + | * @return |
| */ | */ | ||
| - | public function insert(array $inputs):bool | + | public function insert(array $inputs):ORM |
| { | { | ||
| $row = $this-> | $row = $this-> | ||
| $row-> | $row-> | ||
| $row-> | $row-> | ||
| - | | + | |
| + | return $row; | ||
| } | } | ||
| 行 373: | 行 391: | ||
| * @param mixed $id | * @param mixed $id | ||
| * @param array $inputs | * @param array $inputs | ||
| - | * @return | + | * @return |
| */ | */ | ||
| - | public function update($id, array $inputs):bool | + | public function update($id, array $inputs):ORM |
| { | { | ||
| + | // ApricotではSQLite3.0.8以上の使用を前提としており、トランザクション分離レベルはデフォルト値がDEFERREDです。 | ||
| + | // DEFERRED は最初の読み取り時に共有ロックが掛かります(SQLiteのロックはデータベースロックです)。 | ||
| + | // 従って、version_no読み取り後はトランザクション終了まで他の更新は発生しません。 | ||
| + | // NOTE: 他のデータベースの場合は、ここで行ロックを取得してレコードの検索を行います(select for update) | ||
| $row = $this-> | $row = $this-> | ||
| if ($row===false) | if ($row===false) | ||
| 行 393: | 行 415: | ||
| $row-> | $row-> | ||
| $row-> | $row-> | ||
| - | | + | |
| + | return $row; | ||
| } | } | ||
| 行 399: | 行 422: | ||
| * データ削除 | * データ削除 | ||
| * @param mixed $id | * @param mixed $id | ||
| - | * @return | + | * @return |
| */ | */ | ||
| - | public function delete($id): | + | public function delete($id): |
| { | { | ||
| $row = $this-> | $row = $this-> | ||
| 行 408: | 行 431: | ||
| throw new ApplicationException(__(' | throw new ApplicationException(__(' | ||
| } | } | ||
| - | | + | |
| + | return $row; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * 最新の更新結果の取得(insert/ | ||
| + | * @return bool | ||
| + | */ | ||
| + | public function isSuccess(): | ||
| + | { | ||
| + | return $this-> | ||
| } | } | ||
| } | } | ||