Ground Sunlight

Windowsで作る - PHPプログラミングの開発環境

ユーザ用ツール

サイト用ツール


apricot:app:db-model

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
apricot:app:db-model [2020/05/13 16:44]
y2sunlight [Apricot データベースとモデル]
apricot:app:db-model [2020/05/23 14:58] (現在)
y2sunlight [モデルクラス]
行 17: 行 17:
     * [[apricot:app:validation|Apricot バリデーション]]     * [[apricot:app:validation|Apricot バリデーション]]
     * [[apricot:app:transaction|Apricot トランザクション]]     * [[apricot:app:transaction|Apricot トランザクション]]
-  * [[apricot:ext:top|Apricot 拡張]]+  * [[apricot:ext:middleware|Apricot 拡張]]
  
 \\ \\
行 295: 行 295:
  
 ^メソッド名^機能^ ^メソッド名^機能^
-|for_table()\\ :ORM|ORMオジェクトの取得\\ テーブル名(snake_case)はクラス名(UpperCamelCase)から自動判定します。|+|tableName()\\ :string|テール名の取得\\ テーブル名(snake_case)はクラス名(UpperCamelCase)から自動判定します。
 +|for_table()\\ :ORM|ORMオブジェクトの取得|
 |findAll()\\ :array|全件検索\\ ORMの配列を返します。| |findAll()\\ :array|全件検索\\ ORMの配列を返します。|
 |findOne\\ (int $id):mixed|主キー検索\\ 見つかった場合は ORM を、それ以外は false を返します。| |findOne\\ (int $id):mixed|主キー検索\\ 見つかった場合は ORM を、それ以外は false を返します。|
 |create\\ (array $inputs=null):ORM|モデルの新規作成| |create\\ (array $inputs=null):ORM|モデルの新規作成|
-|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()\\ :bool|最新の更新結果を取得します(insert/update/delete)|
  
 ソースコードを以下に示します。 ソースコードを以下に示します。
行 319: 行 321:
 class Model class Model
 { {
 +    /**
 +     * 最新の更新結果(insert/update/delete)
 +     * @var bool
 +     */
 +    private $success = false;
 +
 +    /**
 +     * テーブル名の取得
 +     * @return string
 +     */
 +    public function tableName():string
 +    {
 +        return snake_case(get_short_class_name($this));
 +    }
 +    
     /**     /**
      * テーブルの取得      * テーブルの取得
行 359: 行 376:
      * 新規保存      * 新規保存
      * @param array $inputs      * @param array $inputs
-     * @return bool true on success or false on failure+     * @return \ORM
      */      */
-    public function insert(array $inputs):bool+    public function insert(array $inputs):ORM
     {     {
         $row = $this->for_table()->create($inputs);         $row = $this->for_table()->create($inputs);
         $row->set_expr('created_at', "datetime('now','localtime')");         $row->set_expr('created_at', "datetime('now','localtime')");
         $row->set_expr('updated_at', "datetime('now','localtime')");         $row->set_expr('updated_at', "datetime('now','localtime')");
-        return $row->save();+        $this->success = $row->save()
 +        return $row;
     }     }
  
行 373: 行 391:
      * @param mixed $id      * @param mixed $id
      * @param array $inputs      * @param array $inputs
-     * @return bool true on success or false on failure+     * @return \ORM
      */      */
-    public function update($id, array $inputs):bool+    public function update($id, array $inputs):ORM
     {     {
         // ApricotではSQLite3.0.8以上の使用を前提としており、トランザクション分離レベルはデフォルト値がDEFERREDです。         // ApricotではSQLite3.0.8以上の使用を前提としており、トランザクション分離レベルはデフォルト値がDEFERREDです。
行 397: 行 415:
         $row->set_expr('updated_at', "datetime('now','localtime')");         $row->set_expr('updated_at', "datetime('now','localtime')");
         $row->set_expr('version_no', "version_no+1");         $row->set_expr('version_no', "version_no+1");
-        return $row->save();+        $this->success = $row->save()
 +        return $row;
     }     }
  
行 403: 行 422:
      * データ削除      * データ削除
      * @param mixed $id      * @param mixed $id
-     * @return bool true on success or false on failure+     * @return \ORM
      */      */
-    public function delete($id):bool+    public function delete($id):ORM
     {     {
         $row = $this->for_table()->find_one($id);         $row = $this->for_table()->find_one($id);
行 412: 行 431:
             throw new ApplicationException(__('messages.error.db.delete'));             throw new ApplicationException(__('messages.error.db.delete'));
         }         }
-        return $row->delete();+        $this->success = $row->delete()
 +        return $row; 
 +    } 
 + 
 +    /** 
 +     * 最新の更新結果の取得(insert/update/delete) 
 +     * @return bool 
 +     */ 
 +    public function isSuccess():bool 
 +    { 
 +        return $this->success;
     }     }
 } }
apricot/app/db-model.1589355857.txt.gz · 最終更新: 2020/05/13 16:44 by y2sunlight