3

Zend_Db_Adapter::update() returns the number of rows affected by the update operation. What is best way to determine if the query was successful?

$data = array(
    'updated_on'      => '2007-03-23',
    'bug_status'      => 'FIXED'
); 

$n = $db->update('bugs', $data, 'bug_id = 2');
9
  • If it's not successfull - an exception will be thrown Commented Jul 15, 2012 at 10:59
  • 1
    @zerkms I think that unless there is an adapter issue or an incorrect query that throws a Zend_Db_Statement exception update will return 0 rows if no rows are affected. However you'll likely get a Mysql/pdo error about not enough parameters bound or some such. Commented Jul 15, 2012 at 11:18
  • @RockyFord: if there is an exception - nothing will be returned from that call ever Commented Jul 15, 2012 at 11:22
  • I'm saying it won't always throw an exception. The only exception update throws is for an adapter conflict, every thing else would be peripheral. so assuming your syntax is correct and maybe the id is incorrect, you might get a sql error but you won't get a php exception. Commented Jul 15, 2012 at 11:29
  • @RockyFord: if there will be an sql error - it will be converted to an exception, won't it? "might get a sql error but you won't get a php exception" -- I'm sure there will be a php exception for that ;-) Commented Jul 15, 2012 at 12:09

3 Answers 3

5
$data = array(
    'updated_on' => '2007-03-23',
    'bug_status' => 'FIXED',
);
$n = 0;
try {
    $n = $db->update('bugs', $data, 'bug_id = 2');
} catch (Zend_Exception $e) {
    die('Something went wrong: ' . $e->getMessage());
}
if (empty($n)) {
    die('Zero rows affected');
}
Sign up to request clarification or add additional context in comments.

Comments

3

If you are just looking for a boolean return value, this solution is the best for handling success in the model:

class Default_Model_Test extends Zend_Db_Table {

public function updateTest($testId, $testData){

    try {

        $this->_db->update('test', $testData, array(
            'id = ?'        => $testId
        ));

        return true;

    }
    catch (Exception $exception){

        return false;

    }

}

}

But, a better solution would be to handling success from the controller level, because it is making the request:

class Default_Model_Test extends Zend_Db_Table {

    public function updateTest($testId, $testData){

        $this->_db->update('test', $testData, array(
            'id = ?'        => $testId
        ));

    }

}

class Default_TestController extends Zend_Controller_Action {

    public function updateAction(){

        try {

            $testId = $this->_request->getParam('testId');
            if (empty($testId)) throw new Zend_Argument_Exception('testId is empty');

            $testData = $this->_request->getPost();
            if (empty($testId)) throw new Zend_Argument_Exception('testData is empty');

            $testModel->updateTest($testId, $testData);

        }
        catch (Exception $exception){

            switch (get_class($exception)){

                case 'Zend_Argument_Exception': $message = 'Argument error.'; break;
                case 'Zend_Db_Statement_Exception': $message = 'Database error.'; break;
                case default: $message = 'Unknown error.'; break;

            }

        }

    }

}

This is an excellent solution when working with multiple resource types, using a switch on the exception type, and doing what is appropriate based on your program's needs. Nothing can escape this vacuum.

Comments

0

Maybe:

$data = array(
    'updated_on'      => '2007-03-23',

    'bug_status'      => 'FIXED'
);
$result = $db->update('bugs', $data, 'bug_id = 2');
if ($result < $numRows) {//pass in numRows as method arg or hardcode integer.
    //handle error
} else {
    return TRUE;
}

Try something like this with the idea being you want to verify that the number of records you wanted updated got updated.

1 Comment

number of actually updated rows depends on the original values. So even if $result equals to 0 - then it doesn't mean something wrong happened

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.