Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



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