Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Question about Resultset::update's return value

Hi All,

I have a general question regarding the expected behaviour regarding Phalcon\Mvc\Model\Resultset.

I have a collection of models and I'm updating them like so:

$robots = Robots::find([
    'conditions' => 'category = "toy"',
]);

$result = $robots->update([
    'category' => 'industrial',
]);

My issue is that it appears that regardless of whether the update to the model is succesful, $result is always true.

looking at the source code for Resultset it seems that the update method will always return true even if the transaction failed.

This means that I can't check if my update succeeded using the usual pattern:

if ($result === false) {
   // handle errors
   foreach($robots->getMessages() as $message) {
       echo $message;
   }
}

Is this behaviour intended? If so, what would be the recommended way to check for errrors here?



31.2k
Accepted
answer

Hi Joost maybe you're right looks like a bug Create a new issue

Good luck

edited Aug '19

Thank you, I will create a bug report then.

EDIT: For people encountering the same issue, I filed this problem as issue 14291

EDIT 2: The issue was accepted and fixed in the 4.0.x branch! Many thanks to the Phalcon team and @ruudboon for such a quick response and fix!

If you are still stuck with 3.4.x for whatever reason, you can try the following workaround:

$robots->update([
    'category' => 'industrial',
]);

$messages = $robots->getMessages();
if (count($messages) > 0) {
    // handle errors
    foreach($robots->getMessages() as $message) {
        echo $message;
    }
}

It's a bit hacky, but should do the job until you are able to upgrade.