-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Closed
Feature
Copy link
Labels
new feature requestPlanned Feature or New Feature RequestPlanned Feature or New Feature Request
Description
How to perform batch modification operations without querying and iterate through modifications
According to the document:
An UPDATE
statement performs the update in two phases:
- Retrieval: If the
UPDATE
has aWHERE
clause, it retrieves all the objects that match these criteria. - Update: Based on the queried objects, it updates the requested attributes and stores them in the database.
This way of operation allows events, virtual foreign keys, and validations to be executed during the updating process. In short, the code:
<?php
$phql = "
UPDATE Invoices
SET
inv_status_flag = 0,
inv_total = 0
WHERE
inv_cst_id > 10";
$result = $this
->modelsManager
->executeQuery($phql)
;
if (false === $result->success()) {
$messages = $result->getMessages();
foreach ($messages as $message) {
echo $message->getMessage();
}
}
is somewhat equivalent to:
<?php
use MyApp\Models\Invoices;
$messages = [];
$invoices = Invoices::find(
[
'conditions' => 'inc_cst_id = :customerId:',
'bind' => [
'customerId' => 10,
],
]
);
foreach ($invoices as $invoice) {
$invoice->inv_status_flag = 0;
$invoice->inv_total = 0;
$result = $invoice->save();
if (false === $result) {
$messages[] = $invoice->getMessages();
}
}
For more details, you can refer to the Phalcon documentation.
Metadata
Metadata
Assignees
Labels
new feature requestPlanned Feature or New Feature RequestPlanned Feature or New Feature Request