We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38878
    • 255 Posts
    I am trying to modify the data returned to include category information from a mapping table. I have managed to create and update these category records in my reate and updat classes. Now I want to simply render the mapped categories in a supercombobox in my create and update forms. I am trying to use prepareRow() to retrieve these but it's bombing:
    class PositionGetListProcessor extends modObjectGetListProcessor {
        public $classKey = 'rtPositions';
        public $languageTopics = array('recroot:default');
        public $defaultSortField = 'title';
        public $defaultSortDirection = 'ASC';
        public $objectType = 'recroot.position';
    
        public function prepareQueryBeforeCount(xPDOQuery $c) {
            $query = $this->getProperty('query');
            if (!empty($query)) {
                $c->where(array(
                    'title:LIKE' => '%'.$query.'%',
                    'OR:description:LIKE' => '%'.$query.'%',
                    'OR:company:LIKE' => '%'.$query.'%',
                ));
            }
            return $c;
        }
        //return existing position categories per position from rtPositionCategoriesMap
        public function prepareRow(xPDOObject $object) {
            $ps = $object->toArray('', false, true);
            $position_id = $object->getProperty('id');
            //this is referencing the alias to rtPositionCategoriesMap
            $criteria = $this->modx->newQuery('PositionCategories');
            $criteria->where(array('position_id' => $position_id));
            $categories= $this->modx->getCollection('PositionCategories', $criteria);
            if ($categories) {
                foreach ($categories as $obj) {
                    $cats[] = $obj->get('category_id');
                }
            }    
            $ps['categories'] = $cats;
            return $ps;
        }
    }
    return 'PositionGetListProcessor';


    What am I missing here?

    Also I read the doc on toArray() and the last option includeRelated which read:
    includeRelated: Describes if and how to include loaded related object fields. 
    * As an integer all loaded related objects in the graph up to that level of depth will be included. 
    * As a string, only loaded related objects matching the JSON graph representation will be included. 
    * As an array, only loaded related objects matching the graph array will be included. 
    * As boolean true, all currently loaded related objects will be included.


    Does this apply? If so how could I use the "As an Array" option? Would this simplify my logic?

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

      • 13428 ☆ A M B ☆
      • 1,031 Posts
      One issue could be in line 22. It is just a try to solve it, since I don't know the model:

      Use the following if you refer to the ID of the object.      
      $position_id = $object->get('id');


      Or use this if you have a property 'id' in the getlist processor call that is different to $object->get('id');
      $position_id = $this->getProperty('id');


      • discuss.answer
        • 38878
        • 255 Posts
        Hey Jako,

        The model for this particular issue had 3 tables:

        • positions
        • categories
        • positioncategorymap

        You were basically right. The position_id wasn't defined that way. I got it working by setting the object array in the bolded statement below. My prepareRow now looks like this:
        //return existing position categories per position from rtPositionCategoriesMap
            public function prepareRow(xPDOObject $object) {
                $ps = parent::prepareRow($object); //<- this sets the array to be modified and returned
                $position_id = $ps['id'];
                $criteria = $this->modx->newQuery('rtPositionCategoriesMap');
                $criteria->where(array('position_id' => $position_id,));
                $categories = $this->modx->getCollection('rtPositionCategoriesMap', $criteria);
                if ($categories) {
                    $cats = array();
                    foreach ($categories as $obj) {
                        $cats[] = $obj->get('category_id');
                    }
                }    
                $ps['categories'] = $cats;
                return $ps;
            }


        Now to get the values to render into the supercombobox on load...

        Thanks for your help

        Quote from: Jako at Oct 30, 2015, 07:39 AM
        One issue could be in line 22. It is just a try to solve it, since I don't know the model:

        Use the following if you refer to the ID of the object.
        $position_id = $object->;get('id');


        Or use this if you have a property 'id' in the getlist processor call that is different to $object->get('id');
        $position_id = $this->getProperty('id');