<p>Sleeping on it (and peeking at the db) provided a solution, with even simpler code:</p>
<pre class="lang-php prettyprint-override"><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);
}
</code></pre>
<p>where <code>&quot;1&quot;</code> and <code>&quot;2&quot;</code> are the values associated to the checkboxes.</p>
<p>If you know the checkbox values, use <code>setFieldValue</code> and pass them as an array:</p>
<ul>
<li><code>[]</code> (empty) no checkboxes selected</li>
<li><code>[&quot;single value&quot;]</code></li>
<li><code>[&quot;multi&quot;, &quot;values&quot;]</code></li>
</ul>

CC BY-SA 4.0
2020-12-29T11:18:01.500Z
2020-12-29T23:03:35.887Z
2020-12-29T23:03:35.887Z
pecus
<p>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.</p>
<p>What is the correct way to programmatically set checkboxes (<code>craft\fields\data\MultiOptionsFieldData</code>)?</p>
<p>I verified the migration updates the entry (the title for instance), but not the custom field:</p>
<pre class="lang-php prettyprint-override"><code>$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);
}
</code></pre>
<p>After running the migration, the control panel never shows the checkboxes flagged (the checkbox field has two possible values).</p>
<p>TIA</p>

<craft3><migration><entries-field><checkboxes>
what is the correct way to programmatically set checkboxes (MultiOptionsFieldData) in a migration? (solved)
# what is the correct way to programmatically set checkboxes (MultiOptionsFieldData) in a migration? (solved)

(Asked by: username_0 on 2020-12-29)

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

**Tags:** craft3, migration, entries-field, checkboxes

---

## 1 answers

### Answer by username_0 on 2020-12-29. Score: 2 | Accepted answer

Sleeping on it (and peeking at the db) provided a solution, with even simpler code:
$entries = \craft\elements\Entry::find()
  ->section('sectionProduct')
  ->all();

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

where "1" and "2" 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
["single value"]
["multi", "values"]


---

