Jump to main content Jump to doc navigation

xPDOQuery::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

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • A. Moreno
  • Alex
  • Chris Fickling
  • Stéphane Jäggi
  • YJ
  • Murray Wood
  • JT Skaggs
  • Anton Tarasov
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

$185 per month—let's make that $500!

Learn more