2007年09月22日

Quick Hack to change CakePHP to use object-based models

One of the CakePHP user in Japan wrote an interesting article.

Happy EngineerLife
"Use models of CakePHP as entities"

It explains how to change (or add) a CakePHP's model a "data object".

He added some functions to App_Model and App_Controller and make possible to write this code.


========================================
class ItemsController extends AppController
{
var $name = 'Items';

function index($id) {
$entity = $this->createEntity('Item', $this->Item->read(null, $id));
$this->set('item', $entity);
}
}
========================================


and then you can write your views in this way.

==============================================
//instead of "echo $item['Item']['price'];"
echo $item->g('price'); (or, echo $item->get('price'))
//You can calculate dynamically with Model values.
echo $item->taxOnPrice();
//You can set a value from View.
echo $item->s('price', 5000);
==============================================

posted by SDozono at 14:11| 東京 ☀| Comment(0) | TrackBack(0) | CakePHP Tips | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前: [必須入力]

メールアドレス:

ホームページアドレス:

コメント: [必須入力]

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。

この記事へのトラックバック
手(パー)To make a comment: Please type your name and comment. at the validation field box, type 'cakephp'. And there two glay buttons. "Preview"/"Submit". Thank you!