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'));      
}
4

2 回答 2

3

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

但请确保包含 DboSource:

App::uses('DboSource', 'Model/DataSource');
于 2013-12-03T09:00:27.933 回答
1

表达式的语法是:

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

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

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

它在蛋糕 2.2.x 中工作

于 2013-01-17T23:20:40.783 回答