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!