我的查看操作未显示任何数据。蛋糕PHP

发布于 2024-11-04 17:49:02 字数 762 浏览 6 评论 0 原文

这是我在 newsses/index.ctp 中的链接

$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/view/2”到“newsses/view/title_of_news”

请帮助我......

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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-11-11 17:49:02

您正在使用 Model::read() 方法方法,该方法将您要访问的模型表中的行的 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'));
}

最后,您还可以用(更短的)自定义 findBy 方法替换我示例中的 find 方法(请参阅 文档 了解更多信息)。

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

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);
怂人 2024-11-11 17:49:02

为此,您需要在模型中创建一个新方法,它将按新闻标题显示结果。这时候你使用$this->Newsse->read(NULL,$title))。您在读取方法中使用 $title,而此读取方法在模型中搜索新闻 ID。因此,您只需要在模型类中创建一个新方法,例如 readByTitle($title){ 在此处编写查询以按标题获取新闻}。并在您的控制器中使用此方法。 $this->Newsse->readByTitle(NULL,$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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文