开发者

my view action not showing any data. CakePHP

开发者 https://www.devze.com 2023-03-02 01:36 出处:网络
This my link in newsses/index.ctp $this->Html->link(__(\"Read more >>\", TRUE), array(\'action\'=>\'view\', $newss[\'Newsse\'][\'title\']));

This my link in newsses/index.ctp

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

and this my view code 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->read(NULL,$title));
    $this->开发者_开发知识库;set('newsses', $this->Newsse->find('all'));
}

but it does't showing anything, i want to make route like: "newsses/view/2" to "newsses/view/title_of_news"

please help me....


You're using the Model::read() method method which takes as the second argument the id of the row in your Model's table that you want to access. It's better to use find in this case. You don't need to build a new method in your model or your controller, you can just edit the current view method.

# 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'));
}

Or, you can make a more hybrid form in which viewing by id is still possible when a numerical title is given (this assumes you never have news items which have a title consisting of only numeric characters, e.g. '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'));
}

Finally, you can also replace the find method in my example with a (shorter) custom findBy method (see the documentation for more info about this).

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


For this you need to create a new method in your model Which will display result by news title. At this time your using $this->Newsse->read(NULL,$title)). You are using $title in read method while this read method search against news id in model. So you just need to create a new method in model class like readByTitle($title){ write query here to fetch news by title }. And use this method in your controller. $this->Newsse->readByTitle(NULL,$title))

0

精彩评论

暂无评论...
验证码 换一张
取 消