getSelectColumns breaks query

I’m trying to use getSelectColumns in MODX3, not sure if my approach is wrong, but I’m doing it like this:


$classKey = 'Initiatives\Model\Offenders';

$q = $modx->newQuery($classKey);
$q->select($modx->getSelectColumns($classKey, $classKey, '', ['id', 'title']));
$all = $modx->getCollection($classKey, $q);

foreach($all as $res) {
    echo $res->get('title');
}

and I’m getting

Error 42S22 executing statement: 
Array
(
    [0] => 42S22
    [1] => 1054
    [2] => Unknown column 'Initiatives\Model\Offenders.id' in 'field list'
)

Well, I found it myself :smiley:

$classKey = 'Initiatives\Model\Offenders';

$q = $modx->newQuery($classKey);
$q->select($modx->getSelectColumns($classKey, '', '', ['id', 'title']));
$all = $modx->getCollection($classKey, $q);

foreach($all as $res) {
    print_r($res->toArray('',false,true));
}

seems to work! Looks like $tableAlias doesn’t play well with MODX3.

Interesting. I use joins in queries, and the table alias works fine, but I haven’t used a variable for the alias, just a hand-entered string. Maybe it’s the backslashes that don’t work for aliases. I wonder if you make the alias by grabbing the text after the last backslash [ $tableAlias = substr($classKey, (strrpos($classKey, '\\') + 1)); ], so that you end up with “Offenders” for your alias, if that would work.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.