このページの2つのバージョン間の差分を表示します。
次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
apricot:usage:ja:model [2020/07/29 13:27] tanaka 作成 |
apricot:usage:ja:model [2020/08/05 09:43] tanaka [初期設定ファイル] |
||
---|---|---|---|
行 6: | 行 6: | ||
--- // | --- // | ||
- | [[apricot: | + | [[apricot: |
目次 | 目次 | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
行 16: | 行 18: | ||
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
---- | ---- | ||
- | ===== データベースの利用 | + | ===== ORMの設定 ===== |
- | >TODO | + | |
+ | ORマッパーには[[basic-library: | ||
+ | |||
+ | ==== 設定ファイル ==== | ||
+ | |||
+ | {{fa> | ||
+ | <code php idiorm.setting.php> | ||
+ | <?php | ||
+ | /** | ||
+ | * This file contains Idiorm settings. | ||
+ | */ | ||
+ | return | ||
+ | [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ' | ||
+ | [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ], | ||
+ | ], | ||
+ | ]; | ||
+ | </ | ||
+ | |||
+ | * sqlite : 接続設定(apricotではSQLiteを使用します) | ||
+ | * sqlite.db_file --- データベースファイルのパス (既定値は var/ | ||
+ | * sqlite.connection_string --- 接続文字列 | ||
+ | * sqlite.caching --- キャッシングの有無 | ||
+ | * sqlite.logging --- ロギングの有無 | ||
+ | |||
+ | * initial_data : 初期データ (テーブルが空の時に自動設定されます) | ||
+ | * {テーブル名} --- ここではユーザテーブル( user )を指定しています | ||
+ | * exec --- 実行したいSQLを記述します(複数指定可) | ||
+ | * rows --- 行データを設定します(複数指定可) | ||
+ | |||
+ | 接続設定の詳細は以下を参照して下さい:\\ | ||
+ | https:// | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== 初期設定ファイル | ||
+ | |||
+ | データベースの設定は初期設定ファイルで行います。 | ||
+ | |||
+ | {{fa>folder-open-o}} ** / | ||
+ | <code php idiorm.setup.php> | ||
+ | <?php | ||
+ | /** | ||
+ | * Initial setting of ORM (idirom) | ||
+ | */ | ||
+ | return function(): | ||
+ | { | ||
+ | // Prepares the database files | ||
+ | $db_file = config(' | ||
+ | if (!file_exists($db_path=dirname($db_file))) | ||
+ | { | ||
+ | @mkdir($db_path, | ||
+ | } | ||
+ | |||
+ | // Checks if DB file exists | ||
+ | $new_db_file = !file_exists($db_file); | ||
+ | |||
+ | // Connects to database | ||
+ | ORM:: | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | { | ||
+ | // SQL debug logging | ||
+ | Apricot\Log:: | ||
+ | }, | ||
+ | ]); | ||
+ | |||
+ | // | ||
+ | // Creates tables when creating a new DB | ||
+ | // | ||
+ | if ($new_db_file) | ||
+ | { | ||
+ | $sql_text = file_get_sql(assets_dir(' | ||
+ | if (!empty($sql_text)) | ||
+ | { | ||
+ | foreach($sql_text as $sql) | ||
+ | { | ||
+ | ORM:: | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // Creates initial data when a table is empty | ||
+ | // | ||
+ | $initial_data = config(' | ||
+ | if (isset($initial_data)) | ||
+ | { | ||
+ | foreach($initial_data as $key=> | ||
+ | { | ||
+ | if(ORM:: | ||
+ | { | ||
+ | // SQL execution | ||
+ | if (array_key_exists(' | ||
+ | { | ||
+ | $exec = (array)$item[' | ||
+ | foreach($exec as $sql) | ||
+ | { | ||
+ | ORM:: | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Creates new records | ||
+ | if (array_key_exists(' | ||
+ | { | ||
+ | $rows = (array)$item[' | ||
+ | foreach($rows as $row) | ||
+ | { | ||
+ | $row = ORM:: | ||
+ | $row-> | ||
+ | $row-> | ||
+ | $row-> | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Starts SQL log | ||
+ | ORM:: | ||
+ | return true; // Must return true on success | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | 初期設定ファイルでは以下の事をおこないます: | ||
+ | |||
+ | * データベースの保存フォルダが存在しない場合は作成します | ||
+ | * データベースへの接続 | ||
+ | * テーブルの作成 (新しくDBを作った時) | ||
+ | * 初期ユーザの作成 (ユーザテーブルが空の時) | ||
+ | * SQLログの開始 (logging設定がtrueの場合) | ||
+ | |||
+ | \\ | ||
+ | |||
+ | |||
+ | ===== データベースの構築 ===== | ||
+ | |||
+ | Apricotでは以下のユーザテーブルを持っています。 | ||
+ | |||
+ | テーブル名 : **user** | ||
+ | ^カラム名^型^主Key^属性^説明^ | ||
+ | |id|integer|●|autoincrement|ID| | ||
+ | |account|text| |unique not null|アカウント| | ||
+ | |password|text| |not null|パスワード| | ||
+ | |email|text| |not null|Eメールアドレス| | ||
+ | |note|text| | |備考| | ||
+ | |remember_token|text| | |自動ログイン用| | ||
+ | |created_at|text| |not null|作成日| | ||
+ | |updated_at|text| |not null|更新日| | ||
+ | |version_no|integer| |default 0 not null|バージョンNo| | ||
+ | |||
+ | * ユーザ認証は(account, | ||
+ | * remember_tokenは自動ログイン用の認証トークンです | ||
+ | * version_noは楽観的ロックで使用します | ||
\\ | \\ | ||
===== モデル ===== | ===== モデル ===== | ||
- | >TODO | + | |
+ | ORマッパーが使えるようになったので、モデルのベースクラス( Model )を作ります。Modelクラスは必ず継承して使い、以下のメソッドを持ちます。詳しくはソースコードを参照して下さい。 | ||
+ | |||
+ | ^メソッド名^機能^ | ||
+ | |tableName()\\ : | ||
+ | |for_table()\\ : | ||
+ | |findAll()\\ : | ||
+ | |findOne\\ (int $id): | ||
+ | |create\\ (array $inputs=null): | ||
+ | |insert\\ (array $inputs): | ||
+ | |update\\ ($id, array $inputs): | ||
+ | |delete\\ ($id): | ||
+ | |isSuccess()\\ : | ||
\\ | \\ | ||
- | ===== サービスプロバイダー ===== | + | ===== サービスプロバイター ===== |
- | >TODO | + | |
+ | サービスコンテナを使用することで、サービスとサービス間の依存関係を登録しておいて後で取得することができます。例えば、サービスAがモデルBとモデルCを使用しているような場合、サービスコンテナにサービスAを要求すると、自動的にモデルBとCを生成し、それらをサービスAのコンストラクタに与えてサービスAを生成してくれます。 | ||
+ | |||
+ | サービスプロバイターは、アプリケーション内の全てのサービスコンテナを登録し整理する方法を提供してくれます。また、サービスプロバイダーではサービスが取得された時点で遅延登録されるため、アプリケーションのパフォーマンス向上にも寄与します。 | ||
+ | |||
+ | League/ | ||
+ | |||
+ | - League/ | ||
+ | - League/ | ||
+ | |||
+ | Apricotでは、独自のサービスプロバイダーとして '' | ||
\\ | \\ | ||
+ | ==== Providerクラス ==== | ||
+ | |||
+ | 以下に、League/ | ||
+ | |||
+ | {{fa> | ||
+ | <code php Provider.php> | ||
+ | <?php | ||
+ | namespace App; | ||
+ | |||
+ | use League\Container\ServiceProvider\AbstractServiceProvider; | ||
+ | |||
+ | /** | ||
+ | * Provider class for service | ||
+ | */ | ||
+ | class Provider extends AbstractServiceProvider | ||
+ | { | ||
+ | /** | ||
+ | * The provided array is a way to let the container | ||
+ | * know that a service is provided by this service | ||
+ | * provider. Every service that is registered via | ||
+ | * this service provider must have an alias added | ||
+ | * to this array or it will be ignored. | ||
+ | * | ||
+ | * @var array | ||
+ | */ | ||
+ | protected $provides = [ | ||
+ | // Example | ||
+ | ' | ||
+ | ]; | ||
+ | |||
+ | /** | ||
+ | * This is where the magic happens, within the method you can | ||
+ | * access the container and register or retrieve anything | ||
+ | * that you need to, but remember, every alias registered | ||
+ | * within this method must be declared in the `$provides` array. | ||
+ | */ | ||
+ | | ||
+ | { | ||
+ | // Example | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | このクラスは、名前空間Appの直下に存在し、アプリケーションのモデル及びサービスのマップを提供します。現版のApricotでは、モデルはユーザモデル( User )だけで、サービスについては存在しません。モデルやサービスを追加する場合は、上例に習って適宜追加して下さい。 | ||
+ | |||
+ | 現版のApricotでは、サービスは存在しませんが、サービス用として以下のフォルダが予約されています。 | ||
+ | |||
+ | < | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | 尚、League/ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== App\Foundation\Containerクラス ==== | ||
+ | |||
+ | App\Foundation\Containerクラスは、\League\Container\Container クラスを生成し、Apricotのサービスプロバイダー(Provider)を登録したクラスで、シングルトンとして動作します。 | ||
+ | |||
+ | 使用法: ** Container:: | ||
+ | |||
+ | ^メソッド^機能^ | ||
+ | |mixed get(string $id)|識別子idでコンテナのエントリを検索して返します。| | ||
+ | |bool has(string $id)|コンテナが指定された識別子idのエントリを返すことができる場合はtrueを返します。| | ||
+ | |||
+ | {{fa> | ||
+ | <code php Container.php> | ||
+ | <?php | ||
+ | namespace App\Foundation; | ||
+ | |||
+ | use Core\Foundation\Singleton; | ||
+ | use App\Provider; | ||
+ | |||
+ | /** | ||
+ | * Container class for service | ||
+ | * | ||
+ | * @method static Container getInstance(); | ||
+ | * @method static mixed get(string $id) Finds an entry of the container by its identifier and returns it. | ||
+ | * @method static bool has(string $id) Returns true if the container can return an entry for the given identifier. | ||
+ | */ | ||
+ | class Container extends Singleton | ||
+ | { | ||
+ | /** | ||
+ | * Create Container instance. | ||
+ | * @return \League\Container\Container | ||
+ | */ | ||
+ | protected static function createInstance() | ||
+ | { | ||
+ | $container = new \League\Container\Container; | ||
+ | $container-> | ||
+ | return $container; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== サービスコンテナの使用例 ==== | ||
+ | |||
+ | === スタブコントローラ === | ||
+ | |||
+ | サービスコンテナをテストするために、スタブコントローラを以下のように修正します。 | ||
+ | |||
+ | {{fa> | ||
+ | <code php StubController.php> | ||
+ | namespace App\Controllers; | ||
+ | |||
+ | use App\Foundation\Container; | ||
+ | use App\Foundation\Controller; | ||
+ | |||
+ | /** | ||
+ | * Stubコントローラ | ||
+ | */ | ||
+ | class StubController extends Controller | ||
+ | { | ||
+ | /** | ||
+ | * Stub Page | ||
+ | * @return \Core\Foundation\Response | ||
+ | */ | ||
+ | public function index(int $no=null) | ||
+ | { | ||
+ | $title = "Stub {$no}"; | ||
+ | |||
+ | /* | ||
+ | * Example for Container | ||
+ | * @var \App\Models\User $user | ||
+ | */ | ||
+ | $user = Container:: | ||
+ | $userCount = count($user-> | ||
+ | $messages[] = " | ||
+ | |||
+ | return render(' | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * '' | ||
+ | * ユーザモデルの '' | ||
+ | * ユーザ数を表示するために、テンプレート変数 '' | ||
+ | |||
+ | \\ |