メインコンテンツまでスキップ
Version: Fegg 5.x(開発版)

Model::create

Execute Create Table

Description​

public void Model::create( array $fields )

Creates the model's table. Tables are normally defined in site/config/database/*.yaml and created with ./fegg-cli migration; use this only for tables made at run time. Throws Fegg\Database\QueryException when the query fails.

Arguments​

NameData typeDefaultRemarks
$fieldsarrayList of column definitions (below)

Column definition​

Every key must be present.

KeyData typeRemarks
namestringColumn name
typestringColumn type
notnullbooleanAdd NOT NULL
dflt_valuemixedDefault value (null, '' or 0: no default)
pkbooleanAdd PRIMARY KEY
aibooleanAdd AUTOINCREMENT

Example​

Controller
$model = new Model(true, array(
'table' => 'sample',
));

// Create Table "sample" (sample_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT DEFAULT 'none', valid BOOLEAN, created DATETIME, modified DATETIME)
$model->create(array(
array('name' => 'sample_id', 'type' => 'INTEGER', 'notnull' => true, 'dflt_value' => null, 'pk' => true, 'ai' => true),
array('name' => 'title', 'type' => 'TEXT', 'notnull' => false, 'dflt_value' => 'none', 'pk' => false, 'ai' => false),
array('name' => 'valid', 'type' => 'BOOLEAN', 'notnull' => false, 'dflt_value' => null, 'pk' => false, 'ai' => false),
array('name' => 'created', 'type' => 'DATETIME', 'notnull' => false, 'dflt_value' => null, 'pk' => false, 'ai' => false),
array('name' => 'modified', 'type' => 'DATETIME', 'notnull' => false, 'dflt_value' => null, 'pk' => false, 'ai' => false),
));