We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    This code from the Batcher getlist processor is returning all columns instead of the selected ones (with a pretty hefty JSON return):

    /* 
     * @package batcher
     * @subpackage processors
     */
    /* setup default properties */
    $isLimit = !empty($scriptProperties['limit']);
    $isCombo = !empty($scriptProperties['combo']);
    $start = $modx->getOption('start',$scriptProperties,0);
    $limit = $modx->getOption('limit',$scriptProperties,10);
    $sort = $modx->getOption('sort',$scriptProperties,'templatename');
    $dir = $modx->getOption('dir',$scriptProperties,'ASC');
    
    $c = $modx->newQuery('modTemplate');
    $c->leftJoin('modCategory','Category');
    
    $count = $modx->getCount('modTemplate',$c);
    $c->select(array(
        'modTemplate.id',
        'modTemplate.templatename',
        'modTemplate.description',
    ));
    $c->select(array(
        'category_name' => 'Category.category',
    ));
    $c->sortby($sort,$dir);
    if ($isLimit) {
        $c->limit($limit,$start);
    }
    $templates = $modx->getCollection('modTemplate',$c);
    $list = array();
    foreach ($templates as $template) {
        $templateArray = $template->toArray();
        $templateArray['category'] = $template->get('category_name');
        $list[]= $templateArray;
    }
    return $this->outputArray($list,$count);


    Is there a way to get select() to work with a join like this, or should the columns for the return be selected manually?

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

      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
    • discuss.answer
      • 42602
      • 81 Posts
      Hi Bob,

      $object->toArray(); method fetches lazy fields as default behaviour. To get only selected fields, you can use the method with third parameter set to true:

      $template->toArray('', false, true);
        • 3749
        • 24,544 Posts
        Thanks! I'll try that. You've also reminded me that I can use the raw values, which should speed things up a little.

        [update] Thanks again, that works perfectly and shrinks the JSON being passed by about 90%.

        Now if I could just figure out why every ExtJS Ajax request of mine that gets the grid data gets sent twice (Batcher does this too), and how to stop it. [ed. note: BobRay last edited this post 13 years, 5 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