This is a simple question, but I've looked around and couldn't find the answer. How do I extract the number of affected rows from update/insert SQL queries in ZF2?
The following code is working nicely for me (it does update), but I would like to perform proper error checks:
public function updateSomeField($id, $some_field){
$data = array(
'some_field' => $some_field
);
$sql = new Sql($this->dbAdapter);
$update = $sql->update();
$update->table('Table1');
$update->set($data);
$update->where(array('id' => $id));
$statement = $sql->prepareStatementForSqlObject($update);
// need help with the code below...
// got this from here:
// http://stackoverflow.com/questions/11491249/zend-framework-db-update-result
$result = 0;
try {
$result = $statement->execute(); // works fine
} catch (\Exception $e) {
die('Error: ' . $e->getMessage());
}
if (empty($result)) { // not sure if this is applicable??
die('Zero rows affected');
}
return $result; // ideally, I'd like to return $numRows
}
Currently, when successful, $result is an object. I tried to vardump it, but it's not showing up the values.
Any help would be appreciated. Thanks.
Have you tried:
if ( $result->count() === 0 ) {
die('Zero rows affected');
}
? As far as I remember, it counts anything countable, affected_rows included.