How to get result information when updating mysql table with Doctrine ORM in symfony 1.4

Антон Морозов picture Антон Морозов · Jul 24, 2012 · Viewed 14.3k times · Source

I am developing with symfony 1.4 and using Doctrine ORM. After building schema and models i've got some classes for work with database. I can also use Doctrine_query .... The only thing, that i cann`t understend is:

I need to update table.

Doctrine_Query::create()->update('table')->.....->execute().

or

$tbl = new Table();
$tbl->assignIdentifier($id);
if($tbl->load()){
    $tbl->setFieldname('value');
    $tbl->save();
}

how can i understend was it successful result of query or not ? and how much rows was updated.

p.s. the same question is for delete operation.

Answer

j0k picture j0k · Jul 24, 2012

Everything is in the doc for update and delete.

When executing DQL UPDATE and DELETE queries the executing of a query returns the number of affected rows.

See examples:

$q = Doctrine_Query::create()
        ->update('Account')
        ->set('amount', 'amount + 200')
        ->where('id > 200');

$rows = $q->execute();

echo $rows;
$q = Doctrine_Query::create()
        ->delete('Account a')
        ->where('a.id > 3');

$rows = $q->execute();

echo $rows;

This is related to DQL (when you are using doctrine queries). But I think ->save() will return the current object or true/false as @PLB commented.