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

Massive Update Strategy

Hi guys, i've a concern about doing updates with ORM. If i've to update all related records with the same change how shall i approach?

ORM suggests me to apply this change in a FOR loop over every object but this seems to be really inefficient if you consider that it should consist just in a single update instruction on the RDBMS.

EDIT: I've followed the suggested approach found in the manual under Related Records Update as below:


and also with


but i'm getting this in return

Fatal error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::update()

Can you help me?

Thank you Gianluca

edited Feb '15

The result is an instance of Mvc\Resultset . You can do this:

$unseen_notifications = $this->getUser()->getNotifications("seen=0");

foreach ($unseen_notifications as $notification) {
    $notification-update(['seen' => 1]);
    // or
    // $notification->setSeen(1);
    // $notification->update();


Hi Calin, thanks for the reply. I'm tring to optimize the process using a statement to create the relative update query in the rdbms without massive single updates.

Inside docs is written:

Updating related recordsĀ¶ Instead of doing this:


foreach ($robots->getParts() as $part) {
    $part->stock = 100;
    $part->updated_at = time();
    if ($part->update() == false) {
        foreach ($part->getMessages() as $message) {
            echo $message;

you can do this:


    'stock' => 100,
    'updated_at' => time()

So what's happening with my code? I'm missing something :(



First, you shouldn't put it in an if statement and second the update can fail if:

A) There are no notifications B) There are notifications but the relation in your model is wrong.

You could try my code. If that is not working, probably you should look more into point B)


couldn't you do smth like

$query = new Phalcon\Mvc\Model\Query("update notifications set seen = 1 where seen = 0 && user_id = $id", $this->getDI());

// Execute the query returning a result if any
$cars = $query->execute();

edited Feb '15

As posted here my question seems to be unanswered even if correct. https://forum.phalcon.io/discussion/1290/update-resultset

I don't know why the documentation reports we can call update on a resultset but the class definition is lacking the method.

Can anyone explain this? I agree, it's impossible to accomplish in manual/iterative ways, but the doc says you have not to!

edited Feb '15

simple, someone forgot to add it

you obviously see someone missing out the update function in 1.3.4


Thanks 7thcubic! Is it going to be fixed in 1.3.4? I suppose not... can you confirm this?

1.3.4 has already been released, so it looks like you'll have to wait until 2.0.