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:

        if($this->getUser()->getNotifications("seen=0")->update(array("seen"=>1)))

and also with

        if($this->getUser()->getNotifications()->update(array("seen"=>1)))

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



35.0k
edited Feb '15
$this->getUser()->getNotifications("seen=0")

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();
}


12.1k

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:


<?php

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

you can do this:


<?php

$robots->getParts()->update(array(
    'stock' => 100,
    'updated_at' => time()
));

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



35.0k

Well.

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)



1.5k

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();


12.1k
edited Feb '15

As posted here my question seems to be unanswered even if correct. http://forum.phalconphp.com/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!



1.5k
edited Feb '15

simple, someone forgot to add it

you obviously see someone missing out the update function in 1.3.4



12.1k

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.