Search code examples
databasezend-frameworkzend-db

ZF Db Append to column


Im trying to make an update similar to this with Zend_Db:

UPDATE `TABLE` SET
  column = column + 'new value'
WHERE
  foo = 'bar'

any of you have done this before? is it possible? Thanks


Solution

  • With the help of Zend_Db_Expr this is possible.

    Example:

    $newValue = 101;
    $data     = array('column' =>
                      new Zend_Db_Expr($db->quoteInto('column + ?', $newValue)));
    $where    = $db->quoteInto('foo = ?', 'bar');
    
    $updated = $db->update('TABLE', $data, $where);
    

    Resulting Query:

    UPDATE `TABLE` SET `column` = `column` + 101 WHERE `foo` = 'bar';
    

    If you are asking how to append a string, then the code is similar, but you cannot use the + operator when dealing with character data, instead use CONCAT().

    For example, change the $data array to this:

    $data  = array('varcharCol' =>
                   new Zend_Db_Expr(
                       $db->quoteInto('CONCAT(varcharCol, ?)', ' append more text!')
             ));