【问题标题】:Cakephp Fatal error: Call to a member function format() on a non-objectCakephp 致命错误:在非对象上调用成员函数 format()
【发布时间】:2013-08-06 03:09:50
【问题描述】:

我正在开发 Cakephp 2.3 .. 这是我的代码

  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);

退出();

}

它给了我

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

有人知道我做错了什么吗?

【问题讨论】:

    标签: cakephp datetime timezone cakephp-2.0 cakephp-2.1


    【解决方案1】:

    问题在于,仅仅因为您在控制器中使用 $helper 并不意味着它是可访问的。它实际上被传递给视图或 .ctp 文件。

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

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

    【讨论】:

    • 这个问题之前已经解决了,但是对于不同版本的 cakephp:stackoverflow.com/questions/1779176/cakephp-time-helper-problem
    • 找不到类'TimeHelper'即将到来
    • 你在制作依赖于不同时区的东西吗?否则你可以在 config.php 文件中指定它
    • 好吧,谢谢你的帮助......好吧,我从你给我的链接中找到了一个解决方案,其中有人提到帮助器旨在在视图页面中访问它们,所以现在我正在使用帮助器在视图中......所以现在我在视图页面而不是控制器上转换时间
    • 是的,这更有意义,将所有数据传递到视图然后输出
    猜你喜欢
    • 2012-05-30
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多