findById とか findAllById を使って PK の id を使って取得するパターン
■Model
find 系を使うので特に問題なし
■Controller
/Store?id=10 でアクセスできるようにコントローラを作成
in Controller/StoreController.php
class StoreController extends AppController {
public function index() {
$id = null;
if (isset($this->params['url']['id'])) $id = $this->params['url']['id'];
if ( $id != null ) {
$this->set('Store',$this->Store->findAllById($id));
} else {
$this->set('Store', $this->Store->find('all',array('limit'=>'100')));
}
}
}
パラメータの部分は、$this->params[‘url’][‘id’] のように取得できる。
ただし、?id=10 の部分が指定されていない場合は、エラーになるので、isset でチェック。
findAllById を使っているのは、配列を返したいから。findById にすると単体になるんだけど、View の foreach を変更する必要があり「面倒なので」、1件の場合も配列にするため。
■View
findAllById を使っているので、foreach のところは書き換えずに済む。
■結果
指定した ID の 1件だけが表示される。

