このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
apricot:usage:ja:model [2020/08/03 11:48] tanaka [モデル] |
apricot:usage:ja:model [2020/08/19 11:35] y2sunlight [Idiormの設定ファイル] |
||
---|---|---|---|
行 6: | 行 6: | ||
--- // | --- // | ||
- | [[apricot: | + | [[apricot: |
目次 | 目次 | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
行 16: | 行 18: | ||
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
---- | ---- | ||
- | ===== ORMの設定 | + | ===== ORMの利用 |
- | ORマッパーには[[basic-library: | + | Apricotでは、ORマッパーに[[basic-library: |
+ | |||
+ | Idiormの典型的な使用例を示します: | ||
+ | |||
+ | <code php> | ||
+ | $user = ORM:: | ||
+ | </ | ||
+ | |||
+ | これは、userテーブルから全件を検索する例です。このように、IdiormではORMクラスの静的メソッドの呼び出しを使ってコーディングします。これはApricotのシングルトンと同じ方法で、実際にORMクラスもシングルトンです。 | ||
+ | |||
+ | Apricotのスケルトンで使用しているデータベースはSQLiteです。従って、以下の設定は SQLite の利用を前提としていますのでご注意下さい。データベースを変える場合は、カスタマイズが必要になります。 | ||
+ | |||
+ | \\ | ||
+ | ==== Idiormの設定ファイル ==== | ||
- | ==== 設定ファイル | + | 以下はApricotが提供している初期のIdiormの設定ファイル( '' |
- | {{fa> | + | {{fa> |
<code php idiorm.setting.php> | <code php idiorm.setting.php> | ||
<?php | <?php | ||
+ | /** | ||
+ | * This file contains Idiorm settings. | ||
+ | */ | ||
return | return | ||
[ | [ | ||
行 56: | 行 73: | ||
</ | </ | ||
- | * sqlite | + | '' |
- | * sqlite.db_file --- データベースファイルのパス (既定値は var/ | + | |
- | * sqlite.connection_string --- 接続文字列 | + | * sqlite.db_file --- データベースファイルのパス (初期設定値は var/ |
- | * sqlite.caching --- キャッシングの有無 | + | * sqlite.connection_string --- 接続文字列 |
- | * sqlite.logging --- ロギングの有無 | + | * sqlite.caching --- キャッシングの有無 |
+ | * sqlite.logging --- ロギングの有無 | ||
+ | |||
+ | > Idiormの接続設定の詳細は以下を参照して下さい:\\ https:// | ||
+ | |||
+ | '' | ||
+ | |||
+ | * {テーブル名} --- ここではユーザテーブル( user )を指定しています | ||
+ | * exec --- 実行したいSQLを記述します(複数指定可) | ||
+ | * rows --- 行データを設定します(複数指定可) | ||
+ | |||
+ | |||
+ | |||
+ | |||
- | * initial_data : 初期データ (テーブルが空の時に自動設定されます) | ||
- | * {テーブル名} --- ここではユーザテーブル( user )を指定しています | ||
- | * exec --- 実行したいSQLを記述します(複数指定可) | ||
- | * rows --- 行データを設定します(複数指定可) | ||
- | 接続設定の詳細は以下を参照して下さい:\\ | ||
- | https:// | ||
\\ | \\ | ||
行 79: | 行 104: | ||
<code php idiorm.setup.php> | <code php idiorm.setup.php> | ||
<?php | <?php | ||
- | // | + | /** |
- | // ORM(idirom)の初期設定 | + | * Initial setting of ORM (idirom) |
- | // | + | */ |
return function(): | return function(): | ||
{ | { | ||
- | // データベースファイルの準備 | + | // Prepares the database files |
$db_file = config(' | $db_file = config(' | ||
if (!file_exists($db_path=dirname($db_file))) | if (!file_exists($db_path=dirname($db_file))) | ||
{ | { | ||
- | mkdir($db_path, | + | |
} | } | ||
- | // DBファイルの存在確認 | + | // Checks if DB file exists |
$new_db_file = !file_exists($db_file); | $new_db_file = !file_exists($db_file); | ||
- | // データベース接続 | + | // Connects to database |
ORM:: | ORM:: | ||
' | ' | ||
行 102: | 行 127: | ||
{ | { | ||
// SQL debug logging | // SQL debug logging | ||
- | | + | |
}, | }, | ||
]); | ]); | ||
// | // | ||
- | // テーブルの作成 (新しくDBを作った時) | + | // Creates tables when creating a new DB |
// | // | ||
if ($new_db_file) | if ($new_db_file) | ||
行 122: | 行 147: | ||
// | // | ||
- | // 初期ユーザの作成 (ユーザテーブルが空の時) | + | // Creates initial data when a table is empty |
// | // | ||
$initial_data = config(' | $initial_data = config(' | ||
行 131: | 行 156: | ||
if(ORM:: | if(ORM:: | ||
{ | { | ||
+ | // SQL execution | ||
if (array_key_exists(' | if (array_key_exists(' | ||
{ | { | ||
- | // SQLの実行 | ||
$exec = (array)$item[' | $exec = (array)$item[' | ||
foreach($exec as $sql) | foreach($exec as $sql) | ||
行 140: | 行 165: | ||
} | } | ||
} | } | ||
+ | |||
+ | // Creates new records | ||
if (array_key_exists(' | if (array_key_exists(' | ||
{ | { | ||
- | // 新しいレコードの作成 | ||
$rows = (array)$item[' | $rows = (array)$item[' | ||
foreach($rows as $row) | foreach($rows as $row) | ||
行 156: | 行 182: | ||
} | } | ||
- | // SQLログ開始 | + | // Starts |
ORM:: | ORM:: | ||
return true; // Must return true on success | return true; // Must return true on success | ||
行 212: | 行 238: | ||
\\ | \\ | ||
+ | ===== サービスプロバイター ===== | ||
+ | |||
+ | サービスコンテナを使用することで、サービスとサービス間の依存関係を登録しておいて後で取得することができます。例えば、サービス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 Apricot\Foundation\Singleton; | ||
+ | use App\Provider; | ||
+ | |||
+ | /** | ||
+ | * Container class for service | ||
+ | * | ||
+ | * @method static Container getInstance() Gets the Container instance. | ||
+ | * @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> | ||
+ | <?php | ||
+ | namespace App\Controllers; | ||
+ | |||
+ | use App\Foundation\Container; | ||
+ | use App\Foundation\Controller; | ||
+ | |||
+ | /** | ||
+ | * Stub Controller | ||
+ | */ | ||
+ | class StubController extends Controller | ||
+ | { | ||
+ | /** | ||
+ | * Index Page for this controller. | ||
+ | * | ||
+ | * @return \Apricot\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(' | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * '' | ||
+ | * ユーザモデルの '' | ||
+ | * ユーザ数を表示するために、テンプレート変数 '' | ||
+ | |||
+ | \\ |