我从这里学习 cakephp代码来自旧版本的 cake 所以一些代码需要更新我有控制器,它将数据传递给视图或布局这里是控制器:
博客控制器.php
<?php
class BlogController extends AppController {
var $name = 'Blog';
var $uses = array('Blog');
// Used when indexing the page (http://yourdomain.com/blog/)
function index($view = null) {
// What to do if a view is set
if (isset($view))
{
//problem is here
$this->set('article', $this->Blog->find("id = $view"));
$this->render('article');
}
else
{
$this->set('articles', $this->Blog->find('all'));
}
}
}
?>
问题是这条线$this->set('article', $this->Blog->find("id = $view"));
如果我用它替换该行将$this->set('article', $this->Blog->find('first'))
始终向我显示第一项并且不会出错我如何更正该行以便我可以使用 id?
布局是 article.ctp 如下
<div id="article">
<h1><?= $article['Blog']['title'] ?></h1>
<p class="date"><?= $article['Blog']['date'] ?></p>
<p class="intro"><?= $article['Blog']['introtext'] ?></p>
<p class="main"><?= $article['Blog']['maintext'] ?></p>
</div>
这是通过单击其中一项将得到的错误:
Notice (8): Undefined index: id = 2 [CORE\Cake\Model\Model.php, line 2666]