We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Quick question, it's nagging me... consider the following code:

    // start add all tags to offer
    
    	$inventory_items = array();
    
    	$tags = $formfields['tags'];
    
    	if(count($tags) < 1 ){
    		$this->modx->log(modX::LOG_LEVEL_ERROR, 'Error, no tags entered! # of tags = '.count($tags));
    		return 'you must enter at least one tag number!';
    	}
    
    	foreach($tags as $tag){
    
    		// get the tag data from another table
    		$tag_item = $this->modx->getObject('InventoryData', array('tag_no' => $tag, 'OR:id:=' => $tag));
    
    		if(!$tag_item){
    			// barf an error if the tag is MIA 
    			return 'Tag number'.$tag.' does not exist!';
    		}
    
    		// create a new object to hold the $tag_item result
    		$tdata = $this->modx->newObject('SpecialOfferTag');
    
    		// convert the $tag_item to an array
    		$tag_data = $tag_item->toArray();
    
    		// tag_id does not actually exist in the InventoryData object
    		$tag_data['tag_id'] = $tag_item->get('id');
    
    		// convert it back to an object ??!!what!!??
    		$tdata->fromArray($tag_data);
    
    		// add it to an array of objects
    		$inventory_items[] = $tdata;
    
    	}
    
    	// add the objects to the $offer object
    	$offer->addMany($inventory_items);
    
    // end add tags




    It's just getting an object from the InventoryData table/class & adding [some of] it to a new table/class 'SpecialOfferTag'... but if you notice, I get the object, convert it to an array create a new object and then convert the array back to an object ! - I feel that once I have the original '$tag_item' object, I should be able to add it directly to the $inventory_items array directly [without all that wasteful conversion]

    But I get an error: "No foreign key definition for parentClass: SpecialOffer using relation alias: InventoryData" ~ which is right, there is no relation [nor do I think should there be, the tables are not technically related, I just need the inventory data for use in another application]

    Any thoughts? [any thoughs as in: " should I be able to do this in one step!"]

    -thanks!

      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      If the fields are the same, you might be able to just change the class_key, then add it -- I kind of doubt if it will work. addMany() also takes a second argument, $alias, which specifies the class of the objects you're adding. I'm guessing that won't work either, but it might be worth a try.

      The code you're using should be fairly fast, so you might just leave it alone, since it works. wink

      [update] Here's another way, though I don't know if it would be any faster:

      function objectToObject($instance, $className) {
          return unserialize(sprintf(
              'O:%d:"%s"%s',
              strlen($className),
              $className,
              strstr(strstr(serialize($instance), '"'), ':')
          ));
      }

      [ed. note: BobRay last edited this post 9 years, 8 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 26503
        • 620 Posts
        Hmmm, so I'm not crazy [go figure!], I do have to do all that conversion back and forth...

        Most of the fields in the SpecialOfferTag table do match, but there are a few extras and some that need data massaged ... maybe an aggregate relation would be called for here...

        Well it does work now - will think about the relation

        -thanks again Bob!


        Quote from: BobRay at Jul 29, 2014, 05:48 AM
        If the fields are the same, you might be able to just change the class_key, then add it -- I kind of doubt if it will work. addMany() also takes a second argument, $alias, which specifies the class of the objects you're adding. I'm guessing that won't work either, but it might be worth a try.

        The code you're using should be fairly fast, so you might just leave it alone, since it works. wink

        [update] Here's another way, though I don't know if it would be any faster:

        function objectToObject($instance, $className) {
            return unserialize(sprintf(
                'O:%d:"%s"%s',
                strlen($className),
                $className,
                strstr(strstr(serialize($instance), '"'), ':')
            ));
        }

          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525