Answers.Body: <p>Matrix blocks are, essentially, standalone elements (just like entries, users and assets) that just happen to have an owner element. As such, you'll want to (re)save the <em>block</em> elements themselves in this case; not the <em>owner</em> element (i.e. the &quot;contact&quot; entry containing the Matrix field).</p>
  <p>Try amending your inner <code>foreach</code> loop to this:</p>
  <pre class="lang-php prettyprint-override"><code>foreach($blocks as $block) {
      if ($block-&gt;type == &quot;subhoofding&quot;) {
          // Set new field value
          $block-&gt;setFieldValue('alignering', 'center');
          // Re-save the block
          \Craft::$app-&gt;getElements()-&gt;saveElement($block);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-23T14:35:18.243Z
Answers.Text: thanks very much Mats.  I wasn't aware of this and can confirm this works!
Answers.UserDisplayName: Tim Bertens
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-23T00:37:53.033Z
Answers.LastActivityDate: 2022-12-23T00:37:53.033Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I'm trying to update a specific field (a dropdown) in an existing block (of type 'subhoofding') in an existing matrix field 'contentBuilder'
  The migration itself runs fine, show the new value as output, but some bizarre reason it gets not saved to the database.</p>
  <pre><code>public function safeUp()
      {
          $sites = Craft::$app-&gt;sites-&gt;getAllSites();
  
          foreach($sites as $site) {
              //craft::dd($site-&gt;id);
              $contact = Entry::find()-&gt;slug('contact')-&gt;siteId($site-&gt;id)-&gt;one();
           
  
              // Read content from matrix field
              $blocks = $contact-&gt;getFieldValue('contentBuilder')-&gt;all();
  
              foreach($blocks as $block) {
                  //craft::dd($block-&gt;type);
                  if ($block-&gt;type == &quot;subhoofding&quot;) {
                      echo(&quot;old value = &quot; . $block-&gt;getFieldValue('alignering') . &quot;\n&quot;);  
                      
                      $block-&gt;setFieldValue('alignering', 'center');
                      echo(&quot;new value = &quot; . $block-&gt;getFieldValue('alignering') . &quot;\n&quot;);
  
                  } 
  
              }
  
  
              // And save the entry contact
              $success = \Craft::$app-&gt;elements-&gt;saveElement($contact);
              if ($success) {
                  echo(&quot;SUCCESS \n&quot;);
              }
  
              echo (&quot;  --- on to next site ----  \n&quot;);
      }
      return true;
  </code></pre>
  
Tags: <matrix><migration>
Title: Updating specific field in from block in matrix field via migration
ThreadText: # Updating specific field in from block in matrix field via migration
  
  (Asked by: username_0 on 2022-12-22)
  
  I'm trying to update a specific field (a dropdown) in an existing block (of type 'subhoofding') in an existing matrix field 'contentBuilder'
  The migration itself runs fine, show the new value as output, but some bizarre reason it gets not saved to the database.
  
  ```
  public function safeUp()
      {
          $sites = Craft::$app->sites->getAllSites();
  
          foreach($sites as $site) {
              //craft::dd($site->id);
              $contact = Entry::find()->slug('contact')->siteId($site->id)->one();
           
  
              // Read content from matrix field
              $blocks = $contact->getFieldValue('contentBuilder')->all();
  
              foreach($blocks as $block) {
                  //craft::dd($block->type);
                  if ($block->type == "subhoofding") {
                      echo("old value = " . $block->getFieldValue('alignering') . "\n");  
                      
                      $block->setFieldValue('alignering', 'center');
                      echo("new value = " . $block->getFieldValue('alignering') . "\n");
  
                  } 
  
              }
  
              // And save the entry contact
              $success = \Craft::$app->elements->saveElement($contact);
              if ($success) {
                  echo("SUCCESS \n");
              }
  
              echo ("  --- on to next site ----  \n");
      }
      return true;
  
  ```
  
  **Tags:** matrix, migration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-12-23. Score: 2 | Accepted answer
  
  Matrix blocks are, essentially, standalone elements (just like entries, users and assets) that just happen to have an owner element. As such, you'll want to (re)save the *block* elements themselves in this case; not the *owner* element (i.e. the "contact" entry containing the Matrix field).
  
  Try amending your inner `foreach` loop to this:
  
  ```php
  foreach($blocks as $block) {
      if ($block->type == "subhoofding") {
          // Set new field value
          $block->setFieldValue('alignering', 'center');
          // Re-save the block
          \Craft::$app->getElements()->saveElement($block);
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: thanks very much username_1.  I wasn't aware of this and can confirm this works!  
  
  ---
  
  