2016-01-22 53 views
0

我已經從CakePHP 1.3遷移到CakePHP 2.x,但在新聞列表中,我收到此錯誤消息:「通知(8):未定義變量」在View中(從CakePHP 1.x遷移到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> 

回答

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> 
+0

嗨@InigoFlores,謝謝。 我已經做了更改,但它不起作用: 未定義的索引:NewsSet – r22s

+0

我認爲這是第二行失敗。請確認'News belongsTo NewsSet'。 –

2

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

+0

感謝格式我的答案 – Sahadev

0

通知(8):未定義的變量:newsSet [APP /查看/ newsSets/view.ctp 19行]

的通知,因爲,在NewsSetsController你的公共職能視圖變量newSet($ newSet)沒有,

傳遞變量來查看一下這個,

$這個 - >設置( '新聞',新聞$);

$這個 - >設置( 'newsSet',$新聞);