2012-12-27 45 views
0

第一個代碼塊拋出錯誤Fatal error: Class 'DboSource' not found,而第二個代碼塊工作得很好。這甘拜下風,都使用同樣的線路,以保存創建場... 1座Cakephp - 找不到'DboSource'類

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

回答

1

表達語法是:

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

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

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

它在蛋糕2.2.x工作

3

那麼,表達式不是一個靜態方法,所以Douglesm的解決方案是更好的方法。但是,由於表達式不能訪問任何屬性並且PHP允許,所以您的代碼也應該可以工作。

確保有DboSource包括在內,但:

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