2011-05-03 48 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,$标题))

相关问题