xPDOQuery.select
Last updated Dec 22nd, 2019 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$185 per month—let's make that $500!
Learn morexPDOQuery::select¶
Specify columns to return from the SQL query.
Syntax¶
xPDOQuery select ([string|array $columns = '*'])
Pass either a comma-separated string or an array of column names. '*' is expanded into the full prefixed column list of the class, so a joined query can hydrate every object. To limit the result set, pass the output of getSelectColumns, which returns a comma-delimited string.
Always include the primary key of the table. Without it xPDO cannot key the objects it hydrates, and lazy-loading repeats until PHP stops on the nesting limit.
Example¶
Get a collection of Boxes, with only the ID and name fields.
$query = $xpdo->newQuery('Box');
$query->select($xpdo->getSelectColumns('Box','Box','',array('id','name')));
$boxes = $xpdo->getCollection('Box',$query);
Use with toArray()¶
toArray() lazy-loads missing fields by default, so it prints more than select() asked for. Set the third parameter to true to limit the array to the selected fields.
$query = $xpdo->newQuery('modUser');
$query->select('id,username');
$users = $xpdo->getCollection('modUser',$query);
foreach ($users as $u) {
print_r($u->toArray()); // will print ALL fields.
print_r($u->toArray('',false,true)); // will print ONLY the selected fields.
}
See Also¶
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$185 per month—let's make that $500!
Learn more










