I need to update existing entries after the creation of a checkbox fields, to apply the default value to everything that was created before the availability of the new field. The code should run in a migration to be deployed along the changes in the project config.
What is the correct way to programmatically set checkboxes (craft\fields\data\MultiOptionsFieldData
)?
I verified the migration updates the entry (the title for instance), but not the custom field:
$entries = \craft\elements\Entry::find()
->section('sectionProduct')
->all();
foreach($entries as $entry) {
$options = $entry->distribution->getOptions();
foreach($options as $opt) {
$opt->selected = true;
}
$entry->distribution->setOptions($options);
Craft::$app->getElements()->saveElement($entry);
}
After running the migration, the control panel never shows the checkboxes flagged (the checkbox field has two possible values).
TIA