Answers.Body: <p>The reason the migration wasn't working was I was trying to apply the whole field as <code>$defaultFilterTable</code> and not the values. The fix was changing <code>$defaultFilterTable</code> to <code>$defaultFilterTable-&gt;defaults</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-22T14:01:53.593Z
Answers.LastActivityDate: 2023-06-22T14:01:53.593Z
Answers.UserDisplayName: Eric Anderson
Body: <p>related to <a href="https://craftcms.stackexchange.com/questions/11835/how-can-i-populate-a-dropdown-via-table-field-type-using-the-default-values?r=SearchResults&amp;s=6%7C40.6454">How can I populate a dropdown via table field type using the default values?</a></p>
  <p>I created a table to be used for ordering a display via the order of the table, with a bunch of default values in place. When I apply the table to already existing categories, none of the default values display, but when I create a new category all the default values are there. This is expected behavior so far, but I am looking for a way to apply the default values to the field existing on pre-existing categories. A migration is currently the best approach I have come across, but I am struggling to get the values applied. The <code>safeUp()</code> for my current migration is</p>
  <pre><code> public function safeUp()
      {
  
          $elementsService = Craft::$app-&gt;getElements();
  
          $catQuery = Category::find()
              -&gt;groupId('not 5');
  
          $fieldsService = Craft::$app-&gt;fields;
          
          $defaultFilterTable = $fieldsService
              -&gt;getFieldByHandle('filterDisplayOrder');
          
          foreach ($catQuery as $cat) {
              $cat-&gt;setFieldValue('filterDisplayOrder', $defaultFilterTable);
              $elementsService-&gt;saveElement($cat);
          }
      }
  </code></pre>
  <p>My thought process was to set the default field as the value of the field on the category but that migration completed with no errors and no default values applied on the fields on the category. Thanks in advance.</p>
  
Tags: <migration><table-field>
Title: Get default field values on pre-existing elements
ThreadText: # Get default field values on pre-existing elements
  
  (Asked by: username_0 on 2023-06-22)
  
  related to [How can I populate a dropdown via table field type using the default values?](https://craftcms.stackexchange.com/questions/11835/how-can-i-populate-a-dropdown-via-table-field-type-using-the-default-values?r=SearchResults&s=6%7C40.6454)
  
  I created a table to be used for ordering a display via the order of the table, with a bunch of default values in place. When I apply the table to already existing categories, none of the default values display, but when I create a new category all the default values are there. This is expected behavior so far, but I am looking for a way to apply the default values to the field existing on pre-existing categories. A migration is currently the best approach I have come across, but I am struggling to get the values applied. The `safeUp()` for my current migration is
  
  ```
   public function safeUp()
      {
  
          $elementsService = Craft::$app->getElements();
  
          $catQuery = Category::find()
              ->groupId('not 5');
  
          $fieldsService = Craft::$app->fields;
          
          $defaultFilterTable = $fieldsService
              ->getFieldByHandle('filterDisplayOrder');
          
          foreach ($catQuery as $cat) {
              $cat->setFieldValue('filterDisplayOrder', $defaultFilterTable);
              $elementsService->saveElement($cat);
          }
      }
  
  ```
  
  My thought process was to set the default field as the value of the field on the category but that migration completed with no errors and no default values applied on the fields on the category. Thanks in advance.
  
  **Tags:** migration, table-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-06-22. Score: 1 | Accepted answer
  
  The reason the migration wasn't working was I was trying to apply the whole field as `$defaultFilterTable` and not the values. The fix was changing `$defaultFilterTable` to `$defaultFilterTable->defaults`.
  
  
  ---
  
  