コンフィグとかを元にスキーマの構成を変更したい時。
CakeSchemaにはbeforeとafterというコールバックメソッドがあるます。
最初は単純にbeforeでプロパティ書き換えてやればいいんじゃ、と思ったらどうもできない。
というかbeforeの中からは$this->contents*1のように取得できず。*2
この段階では$this->tables->contentsの中に入っているものの、これを書き換えても無理。
なぜならbeforeが呼ばれる時にはすでにスキーマ構築用のsqlは出来上がっていて、もう干渉することは出来ないからorz
何用に使うんだろう…
schema.php
<?php config('hoge.ini'); class HogeSchema extends CakeSchema { var $name = 'Hoge'; var $contents = array( 'id' => array('type' => 'integer', 'length' => 10, 'null' => false, 'key' => 'primary'), 'content' => array('type' => 'text')), ); function __construct($options = array()) { if (Configure::read('Hoge.writeCreated')) { $this->contents['created'] = array('type' => 'datetime'); } parent::__construct($options); } } ?>
ま、コンストラクタ使っとけば確実ってわけですね。
頼りになるやつです。
