Update multiple columns with Doctrine in Symfony

无人久伴 提交于 2019-12-01 17:23:17

问题


I have to update multiple columns in Symfony, but I can nowhere find the solution... So, I'd like to do it in this way:

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

OK, that works, but I have to do it with several columns. I tried something like this, but it doesn't work:

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

回答1:


Try:

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



回答2:


Try

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



回答3:


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
        }
    }

}

}



来源:https://stackoverflow.com/questions/4803436/update-multiple-columns-with-doctrine-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!