phpmodxmodx-revolutionxpdo

Modx: getCollection query is not working


Inside my processor class I have a statement that grabs all the projects from a db table and formats them to be displayed. This method does not work and halts at the getCollection call.

class GlobalLinkSettingsProcessor extends modObjectGetListProcessor{

    public function initialize() {
        return parent::initialize();
    }

    public function process() {
        $result = $this->modx->getCollection('ManagerProjects');
        $project_names = array();
        foreach ($result as $row) {
            $projects = unserialize($row->get('manager_projects'));
            foreach($projects as $short_code => $project) {
                $project_names[] = array('project_name' => $project, 'project_short_code' => $short_code);
            }
        }
        return '{"total":' . count($project_names) . ',"results":' . $this->modx->toJSON($project_names) . ',"success":true}';
    }
...
}

This code that uses plain SQL does work:

class GlobalLinkSettingsProcessor extends modObjectGetListProcessor{

    public function initialize() {
        return parent::initialize();
    }

    public function process() {
        $leadersql = "SELECT * FROM `modx_manager_projects`";
        $query = $this->modx->query($leadersql);
        $project_names = array();
        while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $projects = unserialize($row['manager_projects']);
            foreach($projects as $short_code => $project) {
                $project_names[] = array('project_name' => $project, 'project_short_code' => $short_code);
            }
        };
        return '{"total":' . count($project_names) . ',"results":' . $this->modx->toJSON($project_names) . ',"success":true}';
    }
...
}

I use similar method to the first which saves ManagerProjects and works fine, so I don't think it has to do with the model declaration. I could easily just use the second method above since it seems to work, but I want to use the best method.

What is wrong with the first method?

Is the first method the proper way to implement SQL in the Modx processor? Or is there a better way?


Solution

  • Because you do it wrong! Just see this. The right way to do it, is something like this:

    <?php
    class GlobalLinkSettingsProcessor extends modObjectGetListProcessor{
        public $classKey = 'ManagerProjects';
    
        public function iterate(array $data) {
            $list = array();
            $list = $this->beforeIteration($list);
            $this->currentIndex = 0;
            /** @var xPDOObject|modAccessibleObject $object */
            foreach ($data['results'] as $object) {
                if ($this->checkListPermission && $object instanceof modAccessibleObject && !$object->checkPolicy('list')) continue;
                $projects = unserialize($object->get('manager_projects'));
                foreach($projects as $short_code => $project) {
                    $objectArray = array('project_name' => $project, 'project_short_code' => $short_code);
                    if (!empty($objectArray) && is_array($objectArray)) {
                        $list[] = $objectArray;
                        $this->currentIndex++;
                    }
                }
            }
            $list = $this->afterIteration($list);
            return $list;
        }
    
    }