我的查看操作未显示任何数据。蛋糕PHP
这是我在 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”
请帮助我......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
Model::read()
方法方法,该方法将您要访问的模型表中的行的id
作为第二个参数。在这种情况下最好使用 find。您不需要在模型或控制器中构建新方法,只需编辑当前的view
方法即可。或者,您可以制作一种更加混合的形式,其中在给出数字标题时仍然可以通过 id 进行查看(这假设您从来没有标题仅由数字字符组成的新闻项目,例如“12345”)。
最后,您还可以用(更短的)自定义
findBy
方法替换我示例中的find
方法(请参阅 文档 了解更多信息)。You're using the
Model::read()
method method which takes as the second argument theid
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 currentview
method.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').
Finally, you can also replace the
find
method in my example with a (shorter) customfindBy
method (see the documentation for more info about this).为此,您需要在模型中创建一个新方法,它将按新闻标题显示结果。这时候你使用$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))