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

时间:2015-04-10 19:51:38

标签: php cakephp

尝试使用timehelper $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->时间仅适用于视野。以下是它在模型中的工作原理:

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

1 个答案:

答案 0 :(得分:2)

来自doc:

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

“如果在视图之外需要TimeHelper个功能......”

$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