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

save function

$robot = Robots::findFirst("id=100"); $robot->name = "Biomass"; $robot->save();

Each update to first query, I feel very troublesome @phalcon is there a better way?

edited Sep '14

Hey,

Of course that will update the first query, because you are selecting id 100. I would recommend doing Robots::findFirstById(100) instead. If you had a gender field you could also do: Robots::findFirstByGender("m") -- It fills in the field in the name!

You have these methods available: $robot->update(); $robot->insert(); $robot->save(); // Does either insert or update depending on if you find a record (like you did above)

It looks like you have to change the ID you are selecting when you save if you want to save to another record.



20.5k
Accepted
answer
edited Sep '14

Hey,

Of course that will update the first query, because you are selecting id 100. I would recommend doing Robots::findFirstById(100) instead. If you had a gender field you could also do: Robots::findFirstByGender('m') -- It fills in the field in the name!

You have these methods available: $robot->update(); $robot->insert(); $robot->save(); // Does either insert or update depending on if you find a record (like you did above)

It looks like you have to change the ID you are selecting when you save if you want to save to another record.

Okay, Thank you for your guidance

If you want to update multiple things you can do this:

$Robots::find();
foreach ($robots as $robot) {
  $robot->name = sprintf("Robot_", rand(1, 1000));
  $robot->save();
}