Saving a SuperTable field within a Matrix Field 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:


Get the SuperTable field id:


$matrixBlocks = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('eventPerformances')-&gt;getBlockTypeFields();
$ticketBlockId = $matrixBlocks[2]['id'];



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' =&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 = '';
}



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' =&gt; 'performance',
        'enabled' =&gt; true,
        'fields' =&gt; array(
            'performanceDate' =&gt; date_create($performance['ts']),
            'duration' =&gt; $performance['duration'] / 60,
            'tickets' =&gt; $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!
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-&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];


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-&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);