このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
apricot:app:db-model [2020/05/11 22:12] y2sunlight |
apricot:app:db-model [2020/05/21 10:41] y2sunlight [Apricot データベースとモデル] |
||
---|---|---|---|
行 14: | 行 14: | ||
* Apricot データベースとモデル | * Apricot データベースとモデル | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | + | * [[apricot: |
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | + | * [[apricot: |
\\ | \\ | ||
行 299: | 行 299: | ||
|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: | 行 320: | ||
class Model | class Model | ||
{ | { | ||
+ | /** | ||
+ | * 最新の更新結果(insert/ | ||
+ | * @var bool | ||
+ | */ | ||
+ | private $success = false; | ||
+ | |||
/** | /** | ||
* テーブルの取得 | * テーブルの取得 | ||
行 359: | 行 366: | ||
* 新規保存 | * 新規保存 | ||
* @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: | 行 381: | ||
* @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: | 行 405: | ||
$row-> | $row-> | ||
$row-> | $row-> | ||
- | | + | |
+ | return $row; | ||
} | } | ||
行 399: | 行 412: | ||
* データ削除 | * データ削除 | ||
* @param mixed $id | * @param mixed $id | ||
- | * @return | + | * @return |
*/ | */ | ||
- | public function delete($id): | + | public function delete($id): |
{ | { | ||
$row = $this-> | $row = $this-> | ||
行 408: | 行 421: | ||
throw new ApplicationException(__(' | throw new ApplicationException(__(' | ||
} | } | ||
- | | + | |
+ | return $row; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 最新の更新結果の取得(insert/ | ||
+ | * @return bool | ||
+ | */ | ||
+ | public function isSuccess(): | ||
+ | { | ||
+ | return $this-> | ||
} | } | ||
} | } |