How to pass an SQL expression to an UPDATE by Zend?

2

My MySQL DBMS has a table called elements where one of the fields is called posicao and is of type int (6) .

I need to perform an update (SQL) where the posicao field should be updated with its own value subtracting 1 according to the where clause. The following query (SQL) does this:

UPDATE elementos
SET posicao = (posicao - 1)
WHERE posicao > 9

Converting this query to use in the model, one of the attempts I made was this, but it did not roll:

$db = new Application_Model_DbTable_Elemento();
$dados = array(
'posicao' => (posicao - 1)
);
$where = $db->getAdapter()->quoteInto('posicao > ?', 9);
$db->update($dados, $where);

I believe the problem is in the data set in the $dados array. How would the syntax to execute this UPDATE ?

    
asked by anonymous 14.03.2014 / 23:20

1 answer

1

From what I saw in a response in English , to pass an expression like this to MySQL it is necessary to create an object of type% with%. With this your code would look like this:

$db = new Application_Model_DbTable_Elemento();
$dados = array(
    'posicao' => new Zend_Db_Expr('posicao - 1')
);
$where = $db->getAdapter()->quoteInto('posicao > ?', 9);
$db->update($dados, $where);
    
15.03.2014 / 23:00