CakePHPの履歴

マニュアル
http://book.cakephp.org/

API
http://api.cakephp.org/

使い方

Component使え!

逆引きリファレンス

Viewテンプレート内で別のテンプレートを呼びたい。

libs.view.php View#render($action, $layout, $file) を使う

// 変数の設定
$this->hogehoge = time();
$this->render('yourTemplateName', false);

Session使ってflashMessage

コントローラで

$this->helpers = array(..., 'Session', ...);
function myaction(){
  $this->Session->setFlash('更新したよ');
}


ビューで

// echoいらない
$session->flash()

ページャ、ページング

アクションで

$this->paginate = array('limit' => 20, 'order' => 'hoge');
// $conditionsはfindと同じでよろし
$this->paginate('User', $conditions);

ビューで
$paginator->params() で各パラメータ取得。
page: 現在のページ番号
defaults.limit: 1ページあたりの項目数
count: 総項目数
current: 現在ページに何件表示されているか
pageCount: 総ページ数

実際これだけじゃ足りなくて自前実装になる。Zend_Paginatorの方がまだ楽かも。

DBからの取得

CakePHPモデルは「arrayベース」。全部arrayでやりとりする。Railsのパクりということで、ActiveRecordパターンかと思ったら、なかそうではなかった。行データゲートウェイパターンか。

// 一件取得
$data = $this->User->findById($id);
// データはこんな風になってる。(モデル名含むので注意!)
// $data = array(
//   'User' => 
//    array('id' => 1, 'name' => 'akkun', 'gender' => 'male')
// );


// gender=maleだけ全件取得
$data = $this->User->find('all', 
   array('conditions' => array('gender' => 'male')));

DBからの取得からの更新

$data = $this->User->findById($id);
$data['User']['name'] = 'john smith';
$this->User->save($data);

フォームでPOST

ビューで

$form->create('User', array('url' => '/user/hoge'))

アクションで

if (!empty($this->data['User'])){
  // POSTの場合
}