如何纠正查找(“id = $ view”));在cakephp?

时间:2013-02-07 18:15:07

标签: cakephp find

我正在学习来自here代码的cakephp是来自旧版本的蛋糕所以需要更新一些代码我有控制器将数据传递给视图或这里的布局是控制器:

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

1 个答案:

答案 0 :(得分:2)

你可能想要这个:

    $this->set('article', $this->Blog->find('first', array('conditions' => array('Blog.id' => $view))));

请参阅:Retrieving Your Data - CakePHP Manual