CakePHP のヘルパーからモデルを使う

2010-9-21 15:41
このエントリーをはてなブックマークに追加

CakePHP は MVC モデルです。View の記述を助けるヘルパーから、データやロジックを扱うモデルを直接呼びすことは通常ありません。

しかし、ときにはヘルパーからモデルを直接呼び出すことで、コードの見通しが簡潔になることがあります。ヘルパー内では次のように記述します。

function get_user_color($id) {
  App::import('Model', 'UserColor'); 
  $uc = new UserColor();
  $data = $uc->read(null, $id);
  return $data['UserColors']['color_name'];
}

App::import はコンポーネントからモデルを呼ぶ場合などにも利用できます。

  • ブックマーク : アクセス: 14,035回
  • カテゴリー : PHP
  • キーワード :

コメントはまだありません

No comments yet.

Sorry, the comment form is closed at this time.

33 queries. HTML convert time: 0.068 sec. Powered by WordPress. Valid XHTML
Copyright © 2003-2017 @ futuremix.org ログイン