Cakephp - 找不到类'DboSource'

时间:2012-12-27 04:41:25

标签: cakephp-2.0

第一个代码块抛出错误Fatal error: Class 'DboSource' not found,而第二个代码块正常工作。这打败了我,都使用相同的行来保存创建的字段... 第1座

public function add() {
    if ($this->request->is('post')) {
        $this->request->data['created'] = DboSource::expression('NOW()');
        $this->EbayAccount->create();
        if ($this->EbayAccount->save($this->request->data)) {
            //$this->EbayAccount->id = $this->EbayAccount->getLastInsertID();
            //$this->EbayAccount->saveField('created', DboSource::expression('NOW()'));
            $this->Session->setFlash(__('The ebay account has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The ebay account could not be saved. Please, try again.'));
        }
    }
    $this->set('userId', $this->Auth->user('id'));      
}

第2区

public function add() {
    if ($this->request->is('post')) {
        //$this->request->data['created'] = DboSource::expression('NOW()');
        $this->EbayAccount->create();
        if ($this->EbayAccount->save($this->request->data)) {
            $this->EbayAccount->id = $this->EbayAccount->getLastInsertID();
            $this->EbayAccount->saveField('created', DboSource::expression('NOW()'));
            $this->Session->setFlash(__('The ebay account has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The ebay account could not be saved. Please, try again.'));
        }
    }
    $this->set('userId', $this->Auth->user('id'));      
}

2 个答案:

答案 0 :(得分:3)

嗯,表达式不是静态方法,所以Douglesm的解决方案是更好的方法。但是,由于表达式不访问任何属性而PHP允许,因此您的代码也应该可以正常工作。

确保包含DboSource:

App::uses('DboSource', 'Model/DataSource');

答案 1 :(得分:1)

表达式的语法是:

$db = ConnectionManager::getDataSource('default');

$this->request->data['created'] = $db->expression('SYSDATE()');

如果您想使用SYSDATE()NOW()和其他表达式

它在蛋糕2.2.x中工作