このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 最新のリビジョン 両方とも次のリビジョン | ||
apricot:app:db-model [2020/05/11 21:48] y2sunlight [モデルクラス] |
apricot:app:db-model [2020/05/23 14:55] y2sunlight [モデルクラス] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > | ||
- | |||
- | ----- | ||
- | |||
====== Apricot データベースとモデル ====== | ====== Apricot データベースとモデル ====== | ||
--- // | --- // | ||
行 18: | 行 14: | ||
* Apricot データベースとモデル | * Apricot データベースとモデル | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | + | * [[apricot: |
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | + | * [[apricot: |
\\ | \\ | ||
行 296: | 行 292: | ||
===== モデルクラス ===== | ===== モデルクラス ===== | ||
- | ORマッパーが使えるようになったので、モデルのベースクラス( Model )を作ります。Modelクラスは以下のメソッドを持ちます。詳しくはソースコードを参照して下さい。 | + | ORマッパーが使えるようになったので、モデルのベースクラス( Model )を作ります。Modelクラスは必ず継承して使い、以下のメソッドを持ちます。詳しくはソースコードを参照して下さい。 |
^メソッド名^機能^ | ^メソッド名^機能^ | ||
- | |for_table(): | + | |for_table()\\ : |
- | |findAll(): | + | |findAll()\\ : |
- | |findOne(int $id):mix|主キー検索\\ 見つかった場合は ORM を、それ以外は false を返します| | + | |findOne\\ (int $id):mixed|主キー検索\\ 見つかった場合は ORM を、それ以外は false を返します。| |
- | |create(array $inputs=null): | + | |create\\ (array $inputs=null): |
- | |insert(array $inputs):bool|レコードの挿入| | + | |insert\\ (array $inputs):ORM|レコードの挿入| |
- | |update($id, | + | |update\\ ($id, array $inputs):ORM|レコードの更新\\ レコードが存在しない時、ApplicationExceptionが発生します。\\ 楽観的ロック例外を検知した時、OptimissticLockExceptionが発生します。| |
- | |delete($id): | + | |delete\\ ($id):ORM|レコードの削除\\ レコードが存在しない時、ApplicationExceptionが発生します。| |
+ | |isSuccess()\\ : | ||
ソースコードを以下に示します。 | ソースコードを以下に示します。 | ||
行 323: | 行 320: | ||
class Model | class Model | ||
{ | { | ||
+ | /** | ||
+ | * 最新の更新結果(insert/ | ||
+ | * @var bool | ||
+ | */ | ||
+ | private $success = false; | ||
+ | |||
+ | /** | ||
+ | * テーブル名の取得 | ||
+ | * @return string | ||
+ | */ | ||
+ | public function tableName(): | ||
+ | { | ||
+ | return snake_case(get_short_class_name($this)); | ||
+ | } | ||
+ | | ||
/** | /** | ||
* テーブルの取得 | * テーブルの取得 | ||
行 329: | 行 341: | ||
public function for_table(): | public function for_table(): | ||
{ | { | ||
- | return ORM:: | + | return ORM:: |
} | } | ||
行 363: | 行 375: | ||
* 新規保存 | * 新規保存 | ||
* @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; | ||
} | } | ||
行 377: | 行 390: | ||
* @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) | ||
行 397: | 行 414: | ||
$row-> | $row-> | ||
$row-> | $row-> | ||
- | | + | |
+ | return $row; | ||
} | } | ||
行 403: | 行 421: | ||
* データ削除 | * データ削除 | ||
* @param mixed $id | * @param mixed $id | ||
- | * @return | + | * @return |
*/ | */ | ||
- | public function delete($id): | + | public function delete($id): |
{ | { | ||
$row = $this-> | $row = $this-> | ||
行 412: | 行 430: | ||
throw new ApplicationException(__(' | throw new ApplicationException(__(' | ||
} | } | ||
- | | + | |
+ | return $row; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 最新の更新結果の取得(insert/ | ||
+ | * @return bool | ||
+ | */ | ||
+ | public function isSuccess(): | ||
+ | { | ||
+ | return $this-> | ||
} | } | ||
} | } |