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

After find(all), update(all) records

Hi,

How do you update all records (like 10 or more), after a find all for a user_id for example?

$robot = Robots::find("user_id=100");
$robot->active = 0;
$robot->save();

This one throws "Fatal error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::save()".

On query, this is simple.

UPDATE robots set active = 0 where user_id=100;

Thanks

$robot is resultset. You need to loop over all elements and set active = 0 and then save each other. Cuz you have objects. You could just simply use some rawsql or phql.

I understand. It means, Phalcon has no ORM method to update all records. Your suggestion to loop over is not efficient, if Ihave a million records. Looping it on PHP will consume a lot of memory and then passing it one-by-one to query is bad.

Anyway, I can see the only way is to use rawsql or phql.

Thanks

ORM dont need such a method cuz its pretty stupid imho and no point int it. Also phalcon is actually consuming a low memory when looping thorugh all records cuz its always storing only one object in memory when looping it. Just tell me how its gonna work in ORM? Actually after ::find method save couldnt know that he has records which have user_id = 100. Then it would still need to loop through all records, which is bad idea. So actually good phalcon dont provide such a solution cuz its pretty tough to implement, i guess generating query which would udpate all records at once would cost more then looping through them and save each other.

edited Nov '15

It's pretty simple, an ORM method to do.

UPDATE robots set active = 0 where user_id=100;

Why? it's pretty and based from experience, got this idea from other framework like cakephp. An equivalent of that query is tough to implement? It's just a simple query with an equivalent criteria. :)

Anyway, have a nice day.

Result sets do have a method called 'update' which can be used for this.

https://github.com/phalcon/cphalcon/blob/79a9bfcb8d8eafeb9fe521de42e4e1b50570b10a/phalcon/mvc/model/resultset.zep#L400

However it appears it does iterate through the results one-by-one. Easiest way to implement what you want would be to have all your models extend a base class, and write a method yourself.

It's pretty simple, an ORM method to do.

UPDATE robots set active = 0 where user_id=100;

Why? it's pretty and based from experience, got this idea from other framework like cakephp. An equivalent of that query is tough to implement? It's just a simple query with an equivalent criteria. :)

Anyway, have a nice day.

But its not ORM method, you dont have ANY objects in this sql:

UPDATE robots set active = 0 where user_id=100;

It doesnt take any records, it updates all records where user_id=100. You can do it by phql or rawsql, there is no point in orm to have such a method.

It's pretty simple, an ORM method to do.

UPDATE robots set active = 0 where user_id=100;

Why? it's pretty and based from experience, got this idea from other framework like cakephp. An equivalent of that query is tough to implement? It's just a simple query with an equivalent criteria. :)

Anyway, have a nice day.