We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2060
    • 6 Posts
    The code below is based on a custom Gallery Class. It returns the correct rows total, but no results. Why?

    [tt]
    /* build query */
    $c = $modx->newQuery(’Gallery’);

    if (!empty($query)) {
    $c->where(array(
    ’name:LIKE’ => ’%’.$query.’%’,
    ));
    }

    $count = $modx->getCount(’Gallery’, $c);
    $c->sortby($sort,$dir);
    if ($isLimit) $c->limit($limit,$start);
    $galleries = $modx->getIterator(’Gallery’, $c);

    /* iterate */
    $list = array();
    foreach ($galleries as $gallery) {
    $list[] = $gallery->toArray();
    }
    return $this->outputArray($list, $count); [/tt]

    Output:
    [tt]({"total":"3","results":[]})[/tt]
      • 28215
      • 4,149 Posts
      Does it work if you use getCollection?
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 3749
        • 24,544 Posts
        What does this give you?

        return print_r($list, true); 
          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
          • 2060
          • 6 Posts
          splitting red:

          getCollection() returns the same thing as getIterator().. an empty array

          BobRay:

          getIterator() is returning an empty array so nothing to loop over.
            • 28215
            • 4,149 Posts
            Quote from: arietis at Mar 09, 2011, 10:53 PM

            getCollection() returns the same thing as getIterator().. an empty array

            Then getIterator isn’t the problem; your query is wrong, or you haven’t run $modx->addPackage to add your custom schema to xPDO at runtime.
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 2060
              • 6 Posts
              Shaun,

              Thanks for your quick reply. I correctly ran $modx->addPackage. It turns out it was because of the sort call:

              [tt]$c->sortby($sort,$dir);[/tt]

              ..where $sort equalled a column that wasn’t in my schema.

              Checking the Error log I see now it was reporting the mysql error all along. Guess I wrongly assumed sortby() would return the mysql error to the query object instead.