I'm trying to bulk update records based on a condition but the entries are NOT getting updated even though the migration seems to go through. Both code samples below work locally but NOT in production even though both configs are the same.
I started with this code, in prod it seemed to complete just fine but none of the records were updated.
$elementsService = Craft::$app->getElements();
$entryQuery = \craft\elements\Entry::find();
$entryQuery->section('mysection');
$elementsService->on(
\craft\services\Elements::EVENT_BEFORE_RESAVE_ELEMENT,
static function(\craft\events\BatchElementActionEvent $e) use ($entryQuery) {
if ($e->query === $entryQuery) {
$element = $e->element;
$element->targetField = ($element->conditionalField) ? 'value-1' : 'value-2';
}
}
);
$elementsService->resaveElements(
$entryQuery,
true,
true
);
So I tried to update one entry at a time as explained in the documentation. Each records is processed (I can see it in the console) but still none of them are updated. Again, this also works in local. This method unfortunately results in "too many files open" after a few hundred, but still, according to the loop they should be getting processed one at a time, not at the end, so why aren't they updating?
$elementsService = Craft::$app->getElements();
$entryQuery = \craft\elements\Entry::find();
foreach ($entryQuery->section('mysection')->all() as $entry ) {
$entry->resaving = true;
$entry->targetField = ($entry->conditionalField) ? 'value-1' : 'value-2';
$elementsService->saveElement($entry);
echo "Updated {$entry->title}.\n";
}
I'm running latest Craft 3. Any pointers will be much appreciated! Thanks