We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34183
    • 23 Posts
    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!
      • 4172
      • 5,888 Posts
      try something like that:

      $pics = array();
      $i = 1;
      foreach ($api_value->animalPictures as $animalPictures) {
          $pic = array();
          $pic['image'] = $animalPictures->urlSecureFullsize;
          $pic['MIGX_id'] = $i;
          $pics[] = $pic;
          $i++;
      }
      
      if (!$page->setTVValue('pet_pictures', $modx->toJson($pics))) {
          $modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem saving your TV animalPictures!');
          return false;
      }
      
      [ed. note: Bruno17 last edited this post 10 years, 3 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 34183
        • 23 Posts
        Perfect, thanks Bruno!
        • Whoa. just what i was looking for.
          Kudos for creating a post with a perfect title to find it.
          Just googled it and ended up here on first click smiley


          Thanks!

          Quote from: Bruno17 at Jan 16, 2014, 05:23 AM
          try something like that:

          $pics = array();
          $i = 1;
          foreach ($api_value->animalPictures as $animalPictures) {
              $pic = array();
              $pic['image'] = $animalPictures->urlSecureFullsize;
              $pic['MIGX_id'] = $i;
              $pics[] = $pic;
              $i++;
          }
          
          if (!$page->setTVValue('pet_pictures', $modx->toJson($pics))) {
              $modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem saving your TV animalPictures!');
              return false;
          }
          
            • 43864
            • 151 Posts
            This works great, but it replaces the value. How to I add a new one in the Migx?
              • 46718
              • 39 Posts
              Yeah, I needed this to pull the latest info from another feed that always has the correct data, so I overwrite the values.

              However, that's only because the MIGX_id field is starting at one. I think you could loop through the MIGX array first and find the last MIGX_id, then increment your new id's from there.
                • 43864
                • 151 Posts
                Quote from: audioroger at Oct 20, 2014, 10:32 PM
                However, that's only because the MIGX_id field is starting at one. I think you could loop through the MIGX array first and find the last MIGX_id, then increment your new id's from there.

                Thx for the answer. Any idea how to do that? I'm not a PHP specialist.
                  • 43864
                  • 151 Posts
                  I founded with the help of other resources in this forum. If anyone wants to comment or add something, please feel free.

                  So I get the result out of a form (Formit) and add it in a Migx.

                  $input = $doc->getTVValue('myMigx');
                  $res = $modx->fromJSON($input);
                  $count = count($res);
                  
                  $new = array(
                      'MIGX_id'       => $count + 1,
                      'name'          => $scriptProperties['fields']['name'],
                      'message'       => $scriptProperties['fields']['message'],
                      'email'         => $scriptProperties['fields']['email'],
                      'date'          => $scriptProperties['fields']['date'],
                  );
                  $res[] = $new;
                  
                  if (!$doc->setTVValue('myMigx', $modx->toJson($res))) {
                    $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
                    return false;
                  }
                   
                  return true;
                    • 4172
                    • 5,888 Posts
                    this is looking good!

                    I think you should check for the heighest MIGX_id. The count must not allways be the heighest one.

                    <?php
                    
                    $value = $doc->getTVValue('myMigx');
                    $items = $modx->fromJSON($value);
                    
                    $next_id = 1;
                    //get the next MIGX_id
                    if (is_array($items)){
                        foreach ($items as $item){
                            $id = $modx->getOption('MIGX_id',$item,0)+1;
                            if ($id > $next_id){
                                $next_id = $id;
                            }   
                        }
                    }else{
                        $items = array();
                    }
                     
                    $item = array(
                        'MIGX_id'       => $next_id,
                        'name'          => $hook->getValue('name'),
                        'message'       => $hook->getValue('message'),
                        'email'         => $hook->getValue('email'),
                        'date'          => $hook->getValue('date'),
                    );
                    $items[] = $item;
                     
                    if (!$doc->setTVValue('myMigx', $modx->toJson($items))) {
                      $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
                      return false;
                    }
                      
                    return true;
                    
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 43864
                      • 151 Posts
                      @Bruno17. Thx for the tip. I'm still new to xPDO, so still learning.