what is the correct way to programmatically set checkboxes (MultiOptionsFieldData) in a migration? (solved) 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()
  -&gt;section('sectionProduct')
  -&gt;all();

foreach($entries as $entry) {
  $options = $entry-&gt;distribution-&gt;getOptions();
  foreach($options as $opt) {
    $opt-&gt;selected = true;
  }
  $entry-&gt;distribution-&gt;setOptions($options);
  Craft::$app-&gt;getElements()-&gt;saveElement($entry);
}

After running the migration, the control panel never shows the checkboxes flagged (the checkbox field has two possible values).
TIA
Sleeping on it (and peeking at the db) provided a solution, with even simpler code:
$entries = \craft\elements\Entry::find()
  -&gt;section('sectionProduct')
  -&gt;all();

foreach($entries as $entry) {
  $entry-&gt;setFieldValue('distribution', [&quot;1&quot;, &quot;2&quot;]);
  Craft::$app-&gt;getElements()-&gt;saveElement($entry);
}

where &quot;1&quot; and &quot;2&quot; are the values associated to the checkboxes.
If you know the checkbox values, use setFieldValue and pass them as an array:

[] (empty) no checkboxes selected
[&quot;single value&quot;]
[&quot;multi&quot;, &quot;values&quot;]