Tous les articles

1 min de lectureArticle en anglais

Doctrine DBAL - Count updated rows

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

Cet article date de 2013. Les outils ont évolué depuis : certaines informations peuvent être dépassées.

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