View中的“Notice(8):Undefined variable”(从CakePHP 1.x迁移到2.x)

时间:2016-01-22 13:13:45

标签: cakephp migration cakephp-2.0 cakephp-2.3

我已从CakePHP 1.3迁移到CakePHP 2.x,但在新闻列表中,我收到此错误:

Notice (8): Undefined variable: newsSet [APP/View/newsSets/view.ctp, line 19]

我的控制器是:

class NewsSetsController extends AppController {

    public $name = 'newsSets';
    public $helpers = array('Html', 'Form', 'Session');
    public $uses = array('Client', 'Block', 'NewsSet', 'Curse', 'Gallery', "News");

    public function index() {
        $this->NewsSet->recursive = 0;
        $this->set('newsSets', $this->paginate());
    }

    public function view($id = null) {
        if (!$id) {
            $this->flash(__('Invalid NewsSet', true), array('action'=>'index'));
        }
        $newsset = $this->NewsSet->read(null, $id);
        $block = $this->NewsSet->Curse->Block->findById($newsset['Curse']['block_id']);
        $this->set('block', $block);
        $news =  $this->NewsSet->News->find("all", array('conditions' => array('News.news_set_id' => $id), 'order' => 'News.order ASC, News.created DESC' , 'recursive' => 1));
        $this->set('news', $news);
        $tree = $this->navTree($id, $newsset['NewsSet']['curse_id']);
        $this->set('tree', $tree);
    }
}

我的观点:

<p><i>Creat: <?php echo $newsSet['NewsSet']['created']; ?></i></p>

3 个答案:

答案 0 :(得分:2)

$this->set('news', $news);更改为$this->set('newsSet', $news);

答案 1 :(得分:1)

您已通过发出

向视图发送了$news
$news = $this->NewsSet->News->find("all", array('conditions' => array('News.news_set_id' => $id), 'order' => 'News.order ASC, News.created DESC' , 'recursive' => 1));  
$this->set('news', $news);

但引用$newsSet

以下内容应该有效:

<p><i>Creat: <?php echo $news['News']['created']; ?></i></p>

如果您在NewsNewsSet(即belongsTohasOne)之间设置了正确的关系,则以下内容也应有效。

<p><i>Creat: <?php echo $news['NewsSet']['created']; ?></i></p>

答案 2 :(得分:0)

通知(8):未定义的变量:newsSet [APP / View / newsSets / view.ctp,第19行]

通知因为,NewsSetsController中的公共函数视图中的变量newSet($ newSet)none,

你传递变量来查看这个,

$ this-&gt; set('news',$ news);

$ this-&gt; set('newsSet',$ news);