Model::__construct
テーブルのモデルを作成します
説明
public Model::__construct( [ boolean $is_admin = false, array $options = [] ] )
table、id、visibleが単純な名前でない場合や、bindに不明な型がある場合はInvalidArgumentExceptionを投げます。
引数
| 名前 | データ型 | デフォルト | 備考 |
|---|---|---|---|
| $is_admin | boolean | false | false:読み込みではvisibleカラムが1の行だけを返します(公開ページ向け)。true:すべての行を返します(CMS向け) |
| $options | array | [] | モデルのオプション(下記) |
オプション
| キー | データ型 | 備考 |
|---|---|---|
table | string | テーブル名。setTableNameと同じです |
id | string | 主キーのカラム。setPrimaryと同じです |
visible | string | 公開フラグのカラム(省略可)。setVisibleと同じです |
bind | array | 書き込み時に型を変換するカラム(column => type)。setBindParamと同じです |
table、id、visibleは単純な名前にします。使えるのは英数字と_で、table.columnの形も使えます。ほかのキーは無視します。
例
Controller
use Fegg\Model;
// For public pages (rows whose visible column is 0 are not returned)
$model = new Model(false, array(
// Operate on the `cms_news` table
'table' => 'cms_news',
// Primary key is `news_id`
'id' => 'news_id',
// Public flag is `visible`
'visible' => 'visible',
// Cast these columns on insert and update
'bind' => array(
'image_id' => 'INTEGER',
'file_id' => 'INTEGER',
'post_date' => 'DATETIME',
),
));
// For CMS pages (every row, visible or not)
$admin = new Model(true, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));