這是一個簡單的問題,但我環顧四周,無法找到答案。 如何從ZF2中的更新/插入SQL查詢中提取受影響的行數?zend框架2 SQL更新並插入受影響的行(ZF2)
下面的代碼工作很好,我(它更新),但我想執行適當的錯誤檢查:
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
}
目前,成功的時候,$結果是一個對象。我嘗試了vardump,但沒有顯示出價值。
任何幫助,將不勝感激。謝謝。
'$ result'希望應該是一個類型的'Result'。在這種情況下,這應該可以幫助你:https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Mysqli/Result.php#L150 - 如果沒有,恐怕我幫不了你 – Sam 2013-02-15 08:08:02
是的,就是這樣。感謝您的鏈接。然後可以應用$ affectedRows = $ result-> getAffectedRows(); – dimmy 2013-02-15 08:32:38