0

コードの最初のブロックはエラーをスローしますFatal error: Class 'DboSource' not foundが、コードの 2 番目のブロックは問題なく動作します。これは私を打ち負かします、両方とも同じ行を使用して作成されたフィールドを保存します...ブロック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 のソリューションの方が優れています。ただし、expression は属性にアクセスせず、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()、およびその他の表現を使用する場合

Cake 2.2.xで動作しています

于 2013-01-17T23:20:40.783 に答える