kawamura.jpでタグ「CakePHP」が付けられているもの
CakePHPのモデル内で、独自のバリデーションメソッドを追加する場合に、自分自身や他のモデルを呼びたいときの書き方。
class Hoge extends AppModel { var $validate = array( 'moreValidation' => array( 'rule' => 'moreValidation', ); var $belongsTo = array( 'Hoge2' => array(〜); );
function moreValidation() { $this->find(〜); $this->Hoge2->find(〜); ClassRegistry::init("Hoge3")->find(〜); } }
- 該当モデル自身を呼び出すときは $this->find(〜)
- アソシエーションしてるモデルは $this->Hoge2->find(〜);
- アソシエーションしてないモデルは ClassRegistry::init("Hoge3")->find(〜);
Routerは便利だが、マニュアルの記述だけだと組み込みのhelperとかの対応が出来ない。。。
と思って探したら参考ページ見つけた。これで解決ゾロ!
iPhone用の画面を作成しようとしたのだが、1.2では1.1のWebServicesが使えなくなったようなので、routerのprefixで頑張れとのことらしい。
http://phptips.seesaa.net/article/102607319.html
とりあえず
Router::connect('/i/:controller/:action', array('prefix' => 'iphone'));
としたが、$html->link() がすべて /i/ 経由に変換されてまった。
Router::connect('/:controller/:action');
で直った。
ただ、1.1のWebServicesは「1つのControllerで違うViewを使う」だったのだが、これだとControllerもiPhone用のものを作らないといけない。解決方法探したが見つからず。
とりあえずエイリアスっぽく
function iphone_index($options) {
return $this->index($options);
}
とするしかないのかなぁ。
追記 :
これだと action以下にパラメータがつくときは認識されないので、
Router::connect('/i/:controller/:action/*', array('prefix' => 'iphone'));
とやる必要がありました。
$this->action : アクション名
$this->viewPath : ビューテンプレートを格納しているディレクトリ名
などなど。ほとんど参照できる。