2011-05-03 51 views
0

這是我在newsses /鏈接index.ctp我的看法動作沒有顯示任何數據。 CakePHP的

$this->Html->link(__("Read more >>", TRUE), array('action'=>'view', $newss['Newsse']['title'])); 

,這我認爲代碼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->read(NULL,$title)); 
    $this->set('newsses', $this->Newsse->find('all')); 
} 

,但開不顯示任何東西, 我要讓路線像: 「newsses /查看/ 2」 到 「newsses /查看/ title_of_news」

請幫我....

+0

只是注意,你能說出你的模型新聞和蛋糕應該理解,控制器也將是新聞 – JohnP 2011-05-03 08:44:25

+0

確定..謝謝.... – Sindhu13 2011-05-05 04:09:58

回答

0

您正在使用的需要作爲第二個參數,你要訪問您的型號的表中的行的idModel::read()方法方法。在這種情況下最好使用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')); 
} 

最後,你也可以用(短)定製findBy方法(見documentation有關此更多信息)替換我的例子find方法。

$this->Newsse->findByTitle($title); 
+0

好的,謝謝的,我會嘗試這個... – Sindhu13 2011-05-05 04:14:33

+0

以及如何關於路由,網址參數,我想讓標題作爲參數,我希望標題小寫並且有下劃線,以及控制器中的視圖函數如何讀取參數......? – Sindhu13 2011-05-05 04:16:35

+0

使用默認路由設置,/ newsses/view/this_is_a_title會自動按照您想要的方式工作。 – vindia 2011-05-05 09:27:39

0

爲此,您需要在您的模型中創建一個新方法,它將顯示新聞標題的結果。這時你使用$ this-> Newsse-> read(NULL,$ title))。您在讀取方法中使用$ title,而此讀取方法搜索模型中的新聞ID。所以你只需要在模型類中創建一個新的方法,如readByTitle($ title){在這裏寫查詢以獲取標題新聞}。並在你的控制器中使用這種方法。 $這個 - > Newsse-> readByTitle(NULL,$標題))

相關問題