Answers.Body: <p>Finally figured this out, I had to delve into the matrix field and get the individual block ids for the Supertable fields.</p>
  
  <pre><code>$matrixBlocks = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('eventPerformances')-&gt;getBlockTypeFields();
  $ticketBlockId = (int)$matrixBlocks[2]['id'];
  $linkBlockId = (int)$matrixBlocks[3]['id'];
  
  $ticketsField = Craft::$app-&gt;getFields()-&gt;getFieldById($ticketBlockId);
  $ticketsBlockTypes = SuperTable::$plugin-&gt;getService()-&gt;getBlockTypesByFieldId($ticketsField-&gt;id);
  $ticketsBlockType = $ticketsBlockTypes[0];
  
  $linksField = Craft::$app-&gt;getFields()-&gt;getFieldById($linkBlockId);
  $linkBlockTypes = SuperTable::$plugin-&gt;getService()-&gt;getBlockTypesByFieldId($linksField-&gt;id);
  $linksBlockType = $linkBlockTypes[0];
  </code></pre>
  
  <p>After casting these to integers and then using that value as the <code>type</code> value when saving the matrix block, it all works!</p>
  
  <p>Full code below, hopefully it will help others out in this situation:</p>
  
  <pre><code>$matrixBlocks = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('eventPerformances')-&gt;getBlockTypeFields();
  $ticketBlockId = (int)$matrixBlocks[2]['id'];
  $linkBlockId = (int)$matrixBlocks[3]['id'];
  
  $ticketsField = Craft::$app-&gt;getFields()-&gt;getFieldById($ticketBlockId);
  $ticketsBlockTypes = SuperTable::$plugin-&gt;getService()-&gt;getBlockTypesByFieldId($ticketsField-&gt;id);
  $ticketsBlockType = $ticketsBlockTypes[0];
  
  $linksField = Craft::$app-&gt;getFields()-&gt;getFieldById($linkBlockId);
  $linkBlockTypes = SuperTable::$plugin-&gt;getService()-&gt;getBlockTypesByFieldId($linksField-&gt;id);
  $linksBlockType = $linkBlockTypes[0];
  
  $performancesMatrix = array();
  $ticketsMatrix = array();
  $linksMatrix = array();
  $performances = $event['schedules'][0]['performances'];
  $i = 1;
  
  foreach ($performances as $performance) {
  
       if (array_key_exists('tickets', $performance))
       {
           $t = 1;
  
           foreach ($performance['tickets'] as $ticket) 
           {
  
               $ticketsMatrix['new'.$t] = array(
                   'type' =&gt; $ticketsBlockType-&gt;id,
                   'enabled' =&gt; true,
                   'fields' =&gt; array(
                   'ticketType' =&gt; $ticket['type'],
                   'minPrice' =&gt; (array_key_exists('min_price', $ticket) ? $ticket['min_price'] : ''),
                   'maxPrice' =&gt; (array_key_exists('max_price', $ticket) ? $ticket['max_price'] : ''),
                   'currency' =&gt; (array_key_exists('currency', $ticket) ? $ticket['currency'] : ''),
                   ),
               );
               $t++;
           }
       }
  
      if(array_key_exists('links', $performance))
      {
           $l = 1;
           foreach($performance['links'] as $link) 
           {
  
               $linksMatrix['new'.$l] = array(
                   'type' =&gt; $linksBlockType-&gt;id,
                   'enabled' =&gt; true,
                   'fields' =&gt; array(
                       'linkType' =&gt; (array_key_exists('type', $link) ? $link['type'] : ''),
                       'linkUrl' =&gt; (array_key_exists('url', $link) ? $link['url'] : ''),
                   ),
               );
               $l++;
           }
       }
  
       $performancesMatrix['new'.$i] = array(
           'type' =&gt; 'performance',
           'enabled' =&gt; true,
           'fields' =&gt; array(
               'performanceDate' =&gt; date_create($performance['ts']),
               'duration' =&gt; (array_key_exists('duration', $performance) ? $performance['duration'] / 60 : ''),
               'tickets' =&gt; $ticketsMatrix,
                'performanceLinks' =&gt; $linksMatrix
           ),
       );
       $i++;
   }
  
  $entry-&gt;setFieldValue('eventPerformances', $performancesMatrix);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-25T08:15:14.403Z
Answers.LastActivityDate: 2019-07-25T08:15:14.403Z
Answers.UserDisplayName: jaypratt
Body: <p>I am trying to save a SuperTable field that sits within a Matrix field.</p>
  
  <p>I have followed the docs as far as I can but for some reason the SuperTable data is not getting saved.</p>
  
  <p>My steps are as follows:</p>
  
  <ol>
  <li>Get the SuperTable field id:</li>
  </ol>
  
  <pre><code>$matrixBlocks = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('eventPerformances')-&gt;getBlockTypeFields();
  $ticketBlockId = $matrixBlocks[2]['id'];
  </code></pre>
  
  <ol start="2">
  <li>Check that the field has content and loop over it to get the array of data:</li>
  </ol>
  
  <pre><code>if ($event['schedules'][0]['performances'][0]['tickets']) {
  
      $ticketsMatrix = array();
      $i = 1;
  
      foreach ($event['schedules'][0]['performances'] as $performance)
  
      {
          foreach ($performance['tickets'] as $ticket) {
              $ticketsMatrix['new'.$i] = array(
                  'type' =&gt; $ticketBlockId,
                  'enabled' =&gt; true,
                  'fields' =&gt; array(
                      'ticketType' =&gt; $ticket['type'],
                      'minPrice' =&gt; $ticket['min_price'],
                      'maxPrice' =&gt; $ticket['max_price'],
                      'currency' =&gt; $ticket['currency'],
                  ),
              );
              $i++;
          }
      }
  } else {
  
      $ticketsMatrix = '';
  }
  </code></pre>
  
  <ol start="3">
  <li>Pass this array through when saving the parent Matrix field</li>
  </ol>
  
  <pre><code>$performancesMatrix = array();
  $performances = $event['schedules'][0]['performances'];
  
  $i = 1;
  
  foreach ($performances as $performance) {
  
      $performancesMatrix['new'.$i] = array(
          'type' =&gt; 'performance',
          'enabled' =&gt; true,
          'fields' =&gt; array(
              'performanceDate' =&gt; date_create($performance['ts']),
              'duration' =&gt; $performance['duration'] / 60,
              'tickets' =&gt; $ticketsMatrix
          ),
      );
      $i++;
  }
  </code></pre>
  
  <p>The <code>performanceDate</code> and <code>duration</code> fields get saved as expected but the <code>$ticketsMatrix</code> data is not saving in the nested SuperTable field.</p>
  
  <p>Any assistance would be appreciated!</p>
  
Tags: <craft3><modules><plugin-supertable>
Title: Saving a SuperTable field within a Matrix Field
ThreadText: # Saving a SuperTable field within a Matrix Field
  
  (Asked by: username_0 on 2019-07-23)
  
  I am trying to save a SuperTable field that sits within a Matrix field.
  
  I have followed the docs as far as I can but for some reason the SuperTable data is not getting saved.
  
  My steps are as follows:
  
  1. Get the SuperTable field id:
  
  ```
  $matrixBlocks = Craft::$app->getFields()->getFieldByHandle('eventPerformances')->getBlockTypeFields();
  $ticketBlockId = $matrixBlocks[2]['id'];
  
  ```
  
  2. Check that the field has content and loop over it to get the array of data:
  
  ```
  if ($event['schedules'][0]['performances'][0]['tickets']) {
  
      $ticketsMatrix = array();
      $i = 1;
  
      foreach ($event['schedules'][0]['performances'] as $performance)
  
      {
          foreach ($performance['tickets'] as $ticket) {
              $ticketsMatrix['new'.$i] = array(
                  'type' => $ticketBlockId,
                  'enabled' => true,
                  'fields' => array(
                      'ticketType' => $ticket['type'],
                      'minPrice' => $ticket['min_price'],
                      'maxPrice' => $ticket['max_price'],
                      'currency' => $ticket['currency'],
                  ),
              );
              $i++;
          }
      }
  } else {
  
      $ticketsMatrix = '';
  }
  
  ```
  
  3. Pass this array through when saving the parent Matrix field
  
  ```
  $performancesMatrix = array();
  $performances = $event['schedules'][0]['performances'];
  
  $i = 1;
  
  foreach ($performances as $performance) {
  
      $performancesMatrix['new'.$i] = array(
          'type' => 'performance',
          'enabled' => true,
          'fields' => array(
              'performanceDate' => date_create($performance['ts']),
              'duration' => $performance['duration'] / 60,
              'tickets' => $ticketsMatrix
          ),
      );
      $i++;
  }
  
  ```
  
  The `performanceDate` and `duration` fields get saved as expected but the `$ticketsMatrix` data is not saving in the nested SuperTable field.
  
  Any assistance would be appreciated!
  
  **Tags:** craft3, modules, plugin-supertable
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-07-25. Score: 1
  
  Finally figured this out, I had to delve into the matrix field and get the individual block ids for the Supertable fields.
  
  ```
  $matrixBlocks = Craft::$app->getFields()->getFieldByHandle('eventPerformances')->getBlockTypeFields();
  $ticketBlockId = (int)$matrixBlocks[2]['id'];
  $linkBlockId = (int)$matrixBlocks[3]['id'];
  
  $ticketsField = Craft::$app->getFields()->getFieldById($ticketBlockId);
  $ticketsBlockTypes = SuperTable::$plugin->getService()->getBlockTypesByFieldId($ticketsField->id);
  $ticketsBlockType = $ticketsBlockTypes[0];
  
  $linksField = Craft::$app->getFields()->getFieldById($linkBlockId);
  $linkBlockTypes = SuperTable::$plugin->getService()->getBlockTypesByFieldId($linksField->id);
  $linksBlockType = $linkBlockTypes[0];
  
  ```
  
  After casting these to integers and then using that value as the `type` value when saving the matrix block, it all works!
  
  Full code below, hopefully it will help others out in this situation:
  
  ```
  $matrixBlocks = Craft::$app->getFields()->getFieldByHandle('eventPerformances')->getBlockTypeFields();
  $ticketBlockId = (int)$matrixBlocks[2]['id'];
  $linkBlockId = (int)$matrixBlocks[3]['id'];
  
  $ticketsField = Craft::$app->getFields()->getFieldById($ticketBlockId);
  $ticketsBlockTypes = SuperTable::$plugin->getService()->getBlockTypesByFieldId($ticketsField->id);
  $ticketsBlockType = $ticketsBlockTypes[0];
  
  $linksField = Craft::$app->getFields()->getFieldById($linkBlockId);
  $linkBlockTypes = SuperTable::$plugin->getService()->getBlockTypesByFieldId($linksField->id);
  $linksBlockType = $linkBlockTypes[0];
  
  $performancesMatrix = array();
  $ticketsMatrix = array();
  $linksMatrix = array();
  $performances = $event['schedules'][0]['performances'];
  $i = 1;
  
  foreach ($performances as $performance) {
  
       if (array_key_exists('tickets', $performance))
       {
           $t = 1;
  
           foreach ($performance['tickets'] as $ticket) 
           {
  
               $ticketsMatrix['new'.$t] = array(
                   'type' => $ticketsBlockType->id,
                   'enabled' => true,
                   'fields' => array(
                   'ticketType' => $ticket['type'],
                   'minPrice' => (array_key_exists('min_price', $ticket) ? $ticket['min_price'] : ''),
                   'maxPrice' => (array_key_exists('max_price', $ticket) ? $ticket['max_price'] : ''),
                   'currency' => (array_key_exists('currency', $ticket) ? $ticket['currency'] : ''),
                   ),
               );
               $t++;
           }
       }
  
      if(array_key_exists('links', $performance))
      {
           $l = 1;
           foreach($performance['links'] as $link) 
           {
  
               $linksMatrix['new'.$l] = array(
                   'type' => $linksBlockType->id,
                   'enabled' => true,
                   'fields' => array(
                       'linkType' => (array_key_exists('type', $link) ? $link['type'] : ''),
                       'linkUrl' => (array_key_exists('url', $link) ? $link['url'] : ''),
                   ),
               );
               $l++;
           }
       }
  
       $performancesMatrix['new'.$i] = array(
           'type' => 'performance',
           'enabled' => true,
           'fields' => array(
               'performanceDate' => date_create($performance['ts']),
               'duration' => (array_key_exists('duration', $performance) ? $performance['duration'] / 60 : ''),
               'tickets' => $ticketsMatrix,
                'performanceLinks' => $linksMatrix
           ),
       );
       $i++;
   }
  
  $entry->setFieldValue('eventPerformances', $performancesMatrix);
  
  ```
  
  
  ---
  
  