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 | このブログの読者になる | 更新情報をチェックする
×

この広告は180日以上新しい記事の投稿がないブログに表示されております。