We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46718
    • 39 Posts
    Thanks @Bruno17, that's exactly what I was thinking!

    Quote from: Bruno17 at Oct 21, 2014, 06:29 AM
    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;
    
      • 51122
      • 1 Posts
      Quote from: [email protected] at Jan 15, 2014, 10:27 PM
      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!

      And What a form code that send data to your snippet?
        • 4172
        • 5,888 Posts
        try this:

        <?php
        
        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 = 1;
                $pics = array();
                foreach ($api_value->animalPictures as $animalPictures) {
                    $item = array(
                        'MIGX_id' => $i, 
                        'image' => $animalPictures->urlSecureFullsize
                    );
                    $pics[] = $item;
                    $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;
                }
                unset($pics); //cleanup
            } //endif
        } //end foreach
        
        
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 37143
          • 74 Posts
          Hi Bruno, was hoping u could help, newbie to xpdo as u know, so appreciate any help....

          I'm trying to save a nested migx TV notes on the front end. It's JSON. So I tried to follow the examples set in this thread, and came up with this:

          The error I'm getting is: Fatal error: Call to undefined method Leads_mysql::getTVValue() in ...../modsnippet/48.include.cache.php on line 27

          That line is bolded below. any thoughts? Thanks!

          <?php
          /* AddNote Snippet */
           
          /* Load our class */
          $path = MODX_CORE_PATH . 'components/leads/';
          $result = $modx->addPackage('leads',$path .
              'model/','crm_');
          if (! $result) {
              return 'Failed to add package';
          }
           
          /* Get the existing Quote */
          $leadId = $scriptProperties['leadId'];
          $lead = $modx->getObject('Leads',
              array('name'=>$leadId));
           
          /* Show error message if quote is not found */
          if (empty($lead)) {
              return ('Could not find Lead with ID: ' . $leadId);
          }
          
          if (isset($_POST['submit']) &&
              ($_POST['submit'] == 'SubmitNote')) {
          
              /* Form has been submitted */
          
            [b]  $value = $lead->getTVValue('notes');[/b]
              $items = $modx->fromJSON($value);
          
              $next_id = 1;
              //get the next MIGX_id
              if (is_array($items)){
                foreach ($items as $item){
                  $id = $modx->getOption('notes',$item,0)+1;
                  if ($id > $next_id){
                      $next_id = $id;
                  }   
                 }
              }else{
              $items = array();
             }
            
          $item = array(
              'notes'       => $next_id,
              'note'          => $hook->getValue('note'),
              'author'       => $hook->getValue('author'),
              'date'          => $hook->getValue('date'),
          );
          $items[] = $item;
            
          if (!$lead->setTVValue('notes', $modx->toJson($items))) {
            $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
            return false;
          }
          
          
          } else {
              /* Not a repost, just display the form */
           
              /* The second argument to getChunk() tells MODX
               * to replace the placeholders with the existing
               * quote's values
               */
             
             $output = $modx->getChunk('addNoteTpl',$lead->toArray() );
          }
          return $output;
            • 4172
            • 5,888 Posts
            should be:

            $lead->set('notes', $modx->toJson($items));
            if (!$lead->save()) {
                $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
                return false;
            }
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 4172
              • 5,888 Posts
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 4172
                • 5,888 Posts
                this should be:

                $value = $lead->get('notes');
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 4172
                  • 5,888 Posts
                  Another one:
                  maybe it would be better to store the notes into another mysql-table and use a MIGXdb-TV-field instaed of the MIGX-field.
                  Than it is easier to add/edit/remove single notes:
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 37143
                    • 74 Posts
                    Thanks, I had actually already changed that on my own, lol. and I eventually realized the reference was to FormIt and deleted the post. I'm not using formit though.

                    I will look into your suggestion to add another table, because i'm having trouble getting the other form fields to save (the note, the author, and the date).
                    Thank you very much!




                    Quote from: Bruno17 at Feb 23, 2016, 06:38 AM
                    this should be:

                    $value = $lead->get('notes');
                      • 4172
                      • 5,888 Posts
                      your schema should look something like that, not sure, if you need an author.
                      Isn't it the same as the createdby (user, which hat created the record)?

                      <?xml version="1.0" encoding="UTF-8"?>
                      <model package="leads" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
                          <object class="Leads" table="leads" extends="xPDOSimpleObject">
                              <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                              <field key="email" dbtype="text" phptype="string" null="false" />
                              <field key="phone" dbtype="text" phptype="string" null="true" />
                              <field key="agent" dbtype="text" phptype="string" null="true" />
                              <field key="prop1" dbtype="text" phptype="string" null="true" />
                              <field key="prop2" dbtype="text" phptype="string" null="true" />
                              <field key="prop3" dbtype="text" phptype="string" null="true" />
                              <field key="description" dbtype="text" phptype="string" null="true" />
                              <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <composite alias="Notes" class="LeadNotes" local="id" foreign="lead_id" cardinality="many" owner="local" />         
                          </object>
                          <object class="Sellers" table="sellers" extends="xPDOSimpleObject">
                              <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                              <field key="prop" dbtype="text" phptype="string" null="false" />
                              <field key="agent" dbtype="text" phptype="string" null="false" />
                              <field key="description" dbtype="text" phptype="string" null="false" />
                              <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <composite alias="Notes" class="SellerNotes" local="id" foreign="seller_id" cardinality="many" owner="local" />
                          </object>
                          <object class="LeadNotes" table="leadnotes" extends="xPDOSimpleObject">
                              <field key="lead_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="note" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                              <field key="author" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <aggregate alias="Lead" class="Leads" local="lead_id" foreign="id" cardinality="one" owner="foreign" /> 
                          </object>
                          <object class="SellerNotes" table="leadnotes" extends="xPDOSimpleObject">
                              <field key="seller_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="note" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                              <field key="author" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
                              <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                              <aggregate alias="Seller" class="Sellers" local="seller_id" foreign="id" cardinality="one" owner="foreign" /> 
                          </object>    
                      </model>
                      
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!