2011-01-26 37 views
5

tôi phải cập nhật nhiều cột trong Symfony, nhưng tôi đâu có thể tìm ra giải pháp ... Vì vậy, tôi muốn làm điều đó theo cách này:Cập nhật nhiều cột với Học thuyết trong Symfony

$q = Doctrine_Query::create() 
    ->update('WebusersTable q') 
    ->set('q.login_name','?','John') 
    ->where('q.webuser_id=?',1) 
    ->execute(); 

OK, điều đó có hiệu quả, nhưng tôi phải làm điều đó với một vài cột. tôi đã cố gắng một cái gì đó như thế này, nhưng nó không hoạt động:

$q = Doctrine_Query::create() 
    ->update('WebusersTable q') 
    ->set('q.login_name,q.name','?','kaka,pisa') 
    ->where('q.webuser_id=?',1) 
    ->execute(); 

Trả lời

14

Hãy thử:

$q = Doctrine_Query::create() 
    ->update('WebusersTable q') 
    ->set('q.login_name', 'John') 
    ->set('q.name', 'Another value') 
    ->where('q.webuser_id=?',1) 
    ->execute(); 
+0

Wow! Điều đó hoạt động! Cảm ơn nhiều! :) – kungfucsiga

+0

Gọn gàng - không thấy được sử dụng trước đây. 1 :) – richsage

+0

Làm thế nào để jQuery biết John là một chữ, không phải là một tên colum (tôi hỏi vì có những mẫu như -> set ('amount', 'amount + 200') Và tôi có vấn đề dọc theo những dòng này ... –

1

Hãy thử

$q = Doctrine_Query::create() 
->update('WebusersTable q') 
->set(array('q.login_name' => 'John', 
      'q.name' => 'Another value')) 
->where('q.webuser_id=?',1) 
->execute(); 
0
class contentActions extends sfActions { 

const TABLE_NAME_ARTICLE = 'article'; 

/** 
* Executes index action 
* 
* @param sfRequest $request A request object 
*/ 
public function executeIndex(sfWebRequest $request) { 


    // Get id from $_GET 
    $id = $request->hasParameter('id') ? $request->getParameter('id') : $request->getPostParameter(self::TABLE_NAME_ARTICLE . '[id]'); 

    // Create model active row by id 
    $modelActiveRow = Doctrine::getTable(self::TABLE_NAME_ARTICLE)->find($id); 

    // Verify existence article 
    $this->forward404Unless($modelActiveRow); 

    // Get form name 
    $formName = self::TABLE_NAME_ARTICLE . 'Form'; 

    // Create article form object. Use model article (load data). 
    $this->form = new $formName($modelActiveRow); 

    if ($request->isMethod('post')) { 

     $postData = $request->getParameter(self::TABLE_NAME_ARTICLE); 
     $this->form->bind($postData); 

     if ($this->form->isValid()) { 
      $this->form->save(); 
      $this->getUser()->setFlash('notice', 'Changes were successfully saved.'); 
      // redirect 
     } 
    } 

} 

}

Các vấn đề liên quan