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 はコンポーネントからモデルを呼ぶ場合などにも利用できます。
コメントはまだありません
No comments yet.
Sorry, the comment form is closed at this time.