We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38878
    • 255 Posts
    Yes...another CMP dev question driving me battay... I am trying to map related table data on create and update in my CMP. Below is the much simpler create processor with an aftersave() function to grab the id I need to map and create the mapping table entries (one for each category in the array). The category values are coming from a supercombobox within the JSON correctly as
    "categories":["1","2","3"]


    I am setting the value of categories below in the beforesave function to the above. I'm getting an error because the $categories array is undefined. I can't call the mapping until after save because I need the ID of the new record to map it. How can I pass the value of $categories to the aftersave function?
    class PositionCreateProcessor extends modObjectCreateProcessor {
        public $classKey = 'rtPositions';
        public $languageTopics = array('recroot:default');
        public $objectType = 'recroot.position';
    
        public function beforeSave() {
            $now = new DateTime();
            $this->object->set('createdon',$now->format('Y-m-d h:i:s'));
            $title = $this->getProperty('title');
            $company = $this->getProperty('company');
            $type = $this->getProperty('type');
            $source = $this->getProperty('source');
      	$applylink = $this->getProperty('applylink');
      	$categories = $this->getProperty('categories');
            $description = $this->getProperty('description');
            $expireson = $this->getProperty('expireson');
            if (empty($title)) {$this->addFieldError('title',$this->modx->lexicon('recroot.position_err_ns_title'));}
            if (empty($company)) {$this->addFieldError('company',$this->modx->lexicon('recroot.position_err_ns_company'));}
            if (empty($type)) {$this->addFieldError('type',$this->modx->lexicon('recroot.position_err_ns_type'));}
            if (empty($source)) {$this->addFieldError('source',$this->modx->lexicon('recroot.position_err_ns_source'));}
            if (empty($applylink)) {$this->addFieldError('applylink',$this->modx->lexicon('recroot.position_err_ns_applylink'));}
            if (empty($categories)) {$this->addFieldError('categories',$this->modx->lexicon('recroot.position_err_ns_categories'));}
            if (empty($description)) {$this->addFieldError('description',$this->modx->lexicon('recroot.position_err_ns_description'));}
            if (empty($expireson)) {$this->addFieldError('expireson',$this->modx->lexicon('recroot.position_err_ns_expireson'));}
            return parent::beforeSave();
        }
        
        public function afterSave() {
            $position_id = $this->object->get('id');
            foreach ($categories as $category) { <-- dying here as $categories is undefined
                $newMap = $xpdo->newObject('rtPositionCategoriesMap',array('position_id' => $position_id,'category_id' => $category,));
                $newMap->save();
                $this->modx->log(modX::LOG_LEVEL_DEBUG, 'Mapped Category ' . $category . ' to position ' . $position_id); 
            }
            return true;
        }
    }
    return 'PositionCreateProcessor';


    ANy help would be great, thanks!

    This question has been answered by harveyev. See the first response.

    • discuss.answer
      • 38878
      • 255 Posts
      I figured it out. I just had to do the
      $categories = $this->getProperty('categories');
      in the aftersave function. And of course
      $newMap = $this->modx->newObject('rtPositionCategoriesMap',array('position_id' => $position_id,'category_id' => $category,));
      ... Now onto the update function....