在非对象上调用成员函数 wasWithinLast()

Call to a member function wasWithinLast() on a non-object

正在尝试使用时间助手$this->Time->wasWithinLast($how_often, $last_updated);

但我不断收到

Error: Call to a member function wasWithinLast() on a non-object    

好像找不到$this->Time?对吗?

$how_often$last_updated 都是正确的格式。

解决方案: 这是解决方案。 $this->Time 仅在视图中有效。以下是它在模型中的工作原理:

CakeTime::wasWithinLast($how_often, $last_updated);

这是我的提醒控制器的开始:

class RemindersController extends AppController {

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
    public function index() {
        var_dump($this->Time);
        $this->Reminder->recursive = 0;
        $this->set('reminders', $this->Paginator->paginate());

    }

这是模型:

class Reminder extends AppModel {


    public function beforeSave($options = array())
  {
    // Attribute to this user
    $this->data['Reminder']['user_id'] = AuthComponent::user('id');
    $this->data['Reminder']['how_often'] = $this->data['Reminder']['number'].' '.$this->data['Reminder']['frame'];
    $this->data['Reminder']['last_reminded'] = $this->data['Reminder']['created'];



  }

  public $virtualFields = array(
    'remindable' => 'Reminder.created'
);

   public function afterFind($results, $primary = false){

    parent::afterFind($results, $primary);


    foreach ($results as $key => $val) {
$results[$key]['Reminder']['remindable'] = $this->remindable($results[$key]['Reminder']['how_often'], $results[$key]['Reminder']['last_reminded']);

        // $results[$key]['Reminder']['remindable'] = $this->Time->wasWithinLast($results[$key]['Reminder']['how_often'], $results[$key]['Reminder']['last_reminded']);

// $results[$key]['Comments']


    }


    // $results = Set::sort($results, '{n}.Item.score', 'desc');

    return $results;
}

来自文档:

http://book.cakephp.org/3.0/en/core-libraries/time.html#namespace-Cake\I18n

"If you need TimeHelper functionalities outside of a View..."

$time = new Time('2014-06-15'); // your custom date here
$time->wasWithinLast($how_often, $last_updated);

http://book.cakephp.org/3.0/en/core-libraries/time.html#comparing-with-intervals