Cakephp Fatal error: Call to a member function for

2019-08-31 09:31发布

问题:

i am working on a Cakephp 2.3 .. here is my code

  class MessagesController extends AppController{
     public $helpers = array('Time');

    public function messages(){

    $datetime=  $this->Message->getDatetime($userid); //$datetime = 2013-06-14 10:28:00



    $time = $this->Userinfo->getTimeZone($userid);   //$time = 5.00

   $finaltime =   $this->Time->format('Y-m-d H:i:s', $dateTime, NULL, $time);

exit();

}

it is giving me

Fatal error: Call to a member function format() on a non-object

anybody know what i am doing wrong?

回答1:

The problem is that just because you are using the $helper in the controller doesn't mean it is accessible. It is actually geting passed to the view or .ctp file.

// called as CakeTime
App::uses('CakeTime', 'Utility');
echo CakeTime::format('2011-08-22', '%d-%m-%Y');

Reference: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::format