$modx->newQuery() - understanding sytnax

Summary

I’m working on some navigation within different types of resources based on filters applied (checkboxes, radio buttons etc…) - save to the $_SESSION - needed to avoid using cookies.

I’m in the middle of creating a snippet which as a result should pull all resources that matches filters saved in $_SESSION and now trying to create a dynamic $modx->newQuery() call for these (later on i will find which one is +prev and +next), please see below:

if (isset($filters['status']) || isset($filters['sector']) || isset($filters['fund'])) {
                // Collection filters and formating into query schema
                foreach ($filters as $key => $filter) {
                    $first = true;
                    $filterConditions = [];
                    $c->leftjoin('modTemplateVarResource', $key, '' . $key . '.tmplvarid=' . getTvId('portfolio' . $key) . ' AND ' . $key . '.contentid=modResource.id');
                    foreach ($filter as $idx => $filterVal) {

                        $or = $first ? '' : 'OR:';
                        array_push($filterConditions, array(
                            $or . '' . $key . '.value:LIKE' => '%' . $filterVal . '%'
                        ));
                        $first = false;
                    }
                    array_push($conditions, $filterConditions);
                }
                // Add where
                $c->where($conditions);
                // Add sorting
                $c->leftjoin('modTemplateVarResource', 'portfolioRealisationDate', 'portfolioRealisationDate.tmplvarid=51 AND portfolioRealisationDate.contentid=modResource.id');
                $c->leftjoin('modTemplateVarResource', 'portfolioInvestmentDate', 'portfolioInvestmentDate.tmplvarid=29 AND portfolioInvestmentDate.contentid=modResource.id');
                $c->sortby(
                    // 'status.value', 'ASC',
                    // 'portfolioRealisationDate.value',
                    // 'DESC',
                    // 'portfolioInvestmentDate.value',
                    // 'DESC',
                    // 'menuindex',
                    // 'ASC',
                    // 'pagetitle',
                    // 'ASC'
                    array(
                        'status.value' => 'ASC',
                        'portfolioRealisationDate.value' => 'DESC',
                        'portfolioInvestmentDate.value' => 'DESC',
                        'menuindex' => 'ASC',
                        'pagetitle' => 'ASC'
                    )
                );
                //debug
                $c->prepare();
                return 'SQL: ' . $c->toSQL();
                $collection = $modx->getCollection('modResource', $c);
            }

Wondering if someone has a general way to create dynamic $modx->newQuery() to share and also can confirm me if sortby syntax accepts arrays like above or need to do as per below:

$c->sortby('status.value','ASC');
$c->sortby('portfolioRealisationDate.value','ASC');
$c->sortby('portfolioInvestmentDate.value','ASC');
...etc

If anyone has some tips/guides etc please share them - i think i got a good idea however sometimes I’m lack good practises or correct syntax…

thanks guys!

The array won’t work. You have to call sortby() multiple times.

1 Like

Hello!

Thank you so much for confirming that!