Here is a simple example:
Let’s say I have a MIGX-TV (ID=1) with this structure
[{
"formname":"Form 1"
,"formtabs": [{
"caption":"Tab A"
,"fields": [{
"field":"a1"
,"inputTVtype":"text"
}]
}]
}]
and a second one (ID=2) with this structure
[{
"formname":"Form 2"
,"formtabs": [{
"caption":"Tab B"
,"fields": [{
"field":"b1"
,"inputTVtype":"text"
}]
}]
},
{
"formname":"Form 3"
,"formtabs": [{
"caption":"Tab C"
,"fields": [{
"field":"c1"
,"inputTVtype":"text"
}]
}]
}]
To combine them, I first have to create a new TV (ID=3) that combines the 2 structures:
[{
"formname":"Form 1"
,"formtabs": [{
"caption":"Tab A"
,"fields": [{
"field":"a1"
,"inputTVtype":"text"
}]
}]
},
{
"formname":"Form 2"
,"formtabs": [{
"caption":"Tab B"
,"fields": [{
"field":"b1"
,"inputTVtype":"text"
}]
}]
},
{
"formname":"Form 3"
,"formtabs": [{
"caption":"Tab C"
,"fields": [{
"field":"c1"
,"inputTVtype":"text"
}]
}]
}]
Then to copy the data, I have to create a snippet and execute it once:
<?php
//Read value of TV with ID 1 from Resource with ID 11
$tv1 = $modx->getObject('modTemplateVarResource',array('contentid' => 11,'tmplvarid' => 1));
$data1 = $modx->fromJSON($tv1->get('value'));
//Read value of TV with ID 2 from Resource with ID 22
$tv2 = $modx->getObject('modTemplateVarResource',array('contentid' => 22,'tmplvarid' => 2));
$data2 = $modx->fromJSON($tv2->get('value'));
//To make sure that each item has a unique ID, find the maximum ID in TV1
$max_id = 1;
foreach($data1 as $item1){
if ($item1['MIGX_id'] > $max_id){
$max_id = $item1['MIGX_id'];
}
}
//Add items of TV2 to TV1
foreach($data2 as $item2){
$max_id += 1;
$item2['MIGX_id'] = $max_id; //Change the ID
$data1[] = $item2; //Append to array
}
//Read TV with ID 3 from Resource with ID 33
$tvCombined = $modx->getObject('modTemplateVarResource',array('contentid' => 33,'tmplvarid' => 3));
if (!$tvCombined){
//Create a new TV if it doesn't already exist
$tvCombined = $modx->newObject('modTemplateVarResource');
$tvCombined->set('contentid', 33); //set resource id
$tvCombined->set('tmplvarid', 3); //set tv id
}
//assign the combined data and save the TV
$tvCombined->set('value',$modx->toJSON($data1));
$tvCombined->save();