Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 765
Антон Морозов Avatar asked Dec 05 '22 15:12

Антон Морозов


2 Answers

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.

like image 108
j0k Avatar answered Dec 07 '22 04:12

j0k


$statement = $this->entityManager->getConnection()->prepare($sql);
$statement->execute();

// the counter of rows which are updated
$counter = $statement->rowCount();

It works for me very well with doctrine 2 in symfony 2 project

like image 28
vikbert Avatar answered Dec 07 '22 04:12

vikbert