Hello,
I have a snippet that connects to an API and creates/updates resources from the information imported.
Right now, I am updating standard resource fields and text TV's. However, there are a few objects that I would like to loop through and store in a MIGX TV, but, I'm not sure how to write to a MIGX TV from a snippet and haven't been able to find much information online.
Here's a basic example of what I'm doing now:
foreach ($apiResults->data as $api_value){
$document = $modx->getObject('modResource', array('longtitle' => $api_value->animalID));
if(!empty($document)){ // update existing document
$document = $modx->getObject('modResource',array('longtitle' => $api_value->animalID));
$document->set('pagetitle', $api_value->animalName);
$document->set('content', $api_value->animalDescription);
if ($document->save() === false){ // Save the document
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!');
return false;
}
$page = $modx->getObject('modResource',array('longtitle' => $api_value->animalID));
//this is where my issues are
$i = 0;
$pics = array();
foreach($api_value->animalPictures as $animalPictures){
if($i < 4){
$pics[$i] = $animalPictures->urlSecureFullsize;
if (!$page->setTVValue('pet_picture_'.$i, $pics[$i])) {
$modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your TV animalPictures!');
return false;
}
}
$i++;
}
unset($pics); //cleanup
} //endif
}//end foreach
Currently I have four "picture" TVs called pet_picture_0 - pet_picture_3 and am limiting the results to fit into these. However, sometimes the API returns more than four results. So, ideally I'd like to loop through the $pictures array and write it to a MIGX TV on the current resource.
I'm pretty sure I have to convert it to JSON first, but after that I have no idea what the actual syntax to update the values would be.
Any help is appreciated!