We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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?



32.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.