Michaël Perrin

Sustainable web development.

Doctrine DBAL - Count updated rows

Just a little tip to count how many rows have been updated after the execution of a Doctrine query.

In case you use Doctrine DBAL (typically in a Symfony2 Command in which you may want to keep things simple and optimized) and want to count the number of rows which have been updated during an UPDATE query, the rowCount method can be used on the statement.

Example:

$updateMyTableStmt = $this->em->getConnection()->prepare('
    UPDATE dbo.my_table SET my_col = :old_col_val WHERE my_col = :new_col_val
');

$updateMyTableStmt->bindValue('old_col_val', 'Old value');
$updateMyTableStmt->bindValue('new_col_val', 'New value');
$updateMyTableStmt->execute();

$countUpdated = $updateMyTableStmt->rowCount(); // returns number of updated rows

That was the simple tip of day!

Michaël Perrin

Read more posts by this author.

comments powered by Disqus