2014-10-31 25 views
-1
達到數據庫字段

我無法顯示default.thtml中一個數據庫字段的值,如unreaded消息或用戶名, 我不斷收到未定義的變量:用戶或消息如何從default.thtml中在CakePHP

如何從default.ctp到達他們?

回答

2

由於默認佈局應用廣泛,您將需要處理的AppController.php在beforeFilter方法的數據,所以像這樣

在AppController.php:

public function beforeFilter() { 
    //for example, you want to read messages 
    //import the Model 
    $this->loadModel('Message'); 
    $all_messages = $this->Message->find('all'); //or whatever you need to do to get the data 
    $this->set('all_messages', $all_messages); 
} 

然後在你的default.ctp,調用變量:$ all_messages

+0

非常感謝它的工作原理 – exchanger3145 2014-10-31 11:11:48