Mar 3

逆引きrhaco25: テーブルを定義する(その2)

逆引き24の続きです。

・カラムの入力制限
min

<column name="ir" type="integer" min="10" />
<column name="sg" type="string" min="3" />
<column name="tt" type="text" min="5" />
最小値
integerの場合は最小数値、string, textの場合は最小文字列数(半角)となる。


max
<column name="ir" type="integer" max="100" />
<column name="sg" type="string" max="30" />
<column name="tt" type="text" max="50" />
最大値
integerの場合は最大数値、string, textの場合は最大文字列数(半角)となる。mailでも使用可能(zip, telは使えない)


size
<column name="sg" type="string" size="30" />
<column name="tt" type="text" size="50" />
サイズ
create table時のサイズです。string, textの場合にadminで表示されるフォームの横幅を指定します。またmaxが無い場合は最大文字列数となります。

integerで使えましたっけ?

require
<column name="ir" type="integer" require="true" />
必須要素
trueで必須になる。


requirewith
<column name="ir" type="integer" requirewith="sg" />
<column name="sg" type="string" min="3" />
制限付き必須
この例の場合sgに何か入力されていればirが必須になる。


unique
<column name="ir" type="integer" unique="true" />
ユニーク値
カラム内で唯一の値を取る。


uniquewith
<column name="ir" type="integer" uniquewith="sg" />
<column name="sg" type="string" />
2カラムユニーク
この例の場合irとsgの入力値の組み合わせ(例:ir=1, sg=hoge)が唯一の値となる。


choices
<column name="sg" unique="true" ><choices><data caption="無効">0</data><data caption="有効">1</data></choices></column>
選択
choices内のデータタグ値のみを取る。captionはadmin内で値の代わりに表示される。


max_digits


primary
<column name="ir" type="integer" primary="true" />
プライマリーキー指定


chartype
<column name="sg" type="string" chartype="/^\w+$/" />
正規表現制限
正規表現で指定された制限を持つ。


・リファレンス
テーブル間のリレーションを規定してくれるとてもありがたい機能です。これを指定しておくとfact, flat, dependなど数々の便利な機能を使うことが出来ます。しかもadminで入力フォームを表示する時に自動的にreference先のデータを取得してリスト表示してくれます。リレーションを組む場合は必ず指定すると良いです。

指定方法
<table name="Product>
...
<column name="code" reference="Category.id" />
...
</table>
<table name="Category>
<column name="id" />
...
</table>


使えるようになるメソッド例
fact(親テーブルデータ取得)
$db = new DbUtil(Product::connection());
$products = $db->select(new Product(),new C(Q::fact()));
$products[0]->factCode === カテゴリオブジェクト


depend(子テーブルデータ取得)
$db = new DbUtil(Category::connection());
$categories = $db->select(new Category(),new C(Q::depend()));
$categories[0]->dependCategory === プロダクトオブジェクトの配列



・ラベル
ラベルを指定しておくとadminでnameの代わりに表示してくれます。また自分で表示画面を組む時もテーブルオブジェクト(getした結果のオブジェクトなど)をテンプレートに設定しておけば、ラベルを表示するメソッド(label)を使うことが出来ます。いちいち全てのテンプレートで表示を修正するといったことが無くなります!

指定方法
<column name="ll" label="ラベル" />


labelメソッド
$db = new DbUtil(Product::connection());
$product = $db->get(new Product(1));
$label = $product->label(Product::columnName());


と、こんな感じで機能もりだくさんです。ほとんど何でも出来ると言って良いくらいです!

「project.xmlを変更して、settingボタンを押してテーブル生成ボタンを押す」
これだけです。


簡単ですね!


(ボブ風)

| comment()