Model::read()
アクセスするモデルのテーブル内の行の を 2 番目の引数として取るメソッド method を使用していid
ます。この場合、find を使用することをお勧めします。モデルやコントローラーで新しいメソッドを作成する必要はありません。現在のview
メソッドを編集するだけで済みます。
# in newsses_controller.php:
function view($title = null) {
$this->set('title_for_layout', __('News & Event', true));
if (!$id) {
$this->Session->setFlash(__('Invalid News.', true), 'default', array('class' => 'error'));
$this->redirect(array('action'=>'index'));
}
$this->set('newsse', $this->Newsse->find('first', array(
'conditions' => array('Newsse.title' => $title)
));
$this->set('newsses', $this->Newsse->find('all'));
}
または、数字のタイトルが与えられた場合でも id による表示が可能な、よりハイブリッドなフォームを作成できます (これは、タイトルが数字だけで構成されるニュース項目がないことを前提としています。たとえば、'12345' など)。
# in newsses_controller.php:
function view($title = null) {
$this->set('title_for_layout', __('News & Event', true));
if (!$id) {
$this->Session->setFlash(__('Invalid News.', true), 'default', array('class' => 'error'));
$this->redirect(array('action'=>'index'));
} else if (is_numeric($title)) {
$this->set('newsse', $this->Newsse->read(NULL, $title));
} else {
$this->set('newsse', $this->Newsse->find('first', array(
'conditions' => array('Newsse.title' => $title)
));
}
$this->set('newsses', $this->Newsse->find('all'));
}
最後に、このfind
例のメソッドを (短い) カスタムfindBy
メソッドに置き換えることもできます (詳細については、ドキュメントを参照してください)。
$this->Newsse->findByTitle($title);