I want to update some records in an transaction to like that:
$query = $di->get('modelsManager')->createQuery('UPDATE UserAccounts SET UserAccounts.last_login = UserAccounts.current_login');
$query->execute();
but the official document said that: An UPDATE statement performs the update in two phases:
- First, if the UPDATE has a WHERE clause it retrieves all the objects that match these criteria,
- Second, based on the queried objects it updates/changes the requested attributes storing them to the relational
- database
- This way of operation allows that events, virtual foreign keys and validations take part of the updating process. In summary, the following code:
2.12. Phalcon Query Language (PHQL) 165 Phalcon PHP Framework Documentation, Release 1.3.0
<?php
$phql = "UPDATE Cars SET price = 15000.00 WHERE id > 101";
$success = $manager->executeQuery($phql);
is somewhat equivalent to:
<?php
$messages = null;
$process = function() use (&$messages) {
foreach (Cars::find("id > 101") as $car) {
$car->price = 15000;
if ($car->save() == false) {
$messages = $car->getMessages();
return false;
}
}
return true;
};
$success = $process();
THAT'S NOT TRUE in a transaction! and the foreach clause did a great cost.