xPDOQuery.select
Last edited by Everett Griffiths on Mar 26, 2014.
xPDOQuery::select
Specify columns to return from the SQL query.
Syntax
API Docs: http://api.modx.com/revolution/2.2/db_core_xpdo_om_xpdoquery.class.html#\xPDOQuery::select()
getSelectColumns($className, $tableAlias= '', $columnPrefix= '', $columns= array (), $exclude= false)
xPDOQuery select ([string $columns = '*'])
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()
It's important to point out that toArray() will by default lazy-load values, so it effectively overrides the values you've passed to the select() method. To have toArray() to follow along with what you've passed to select(), you set its third parameter to "true".
$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
The function works with either an array of field names or a comma-separated string:
xPDOQuery select ([string $columns = '*']) xPDOQuery select ([array $columns = array()])
The example in the API docs passes select() the results of getSelectColumns(), which returns a comma-delimited list (a string).
Suggest an edit to this page on GitHub (Requires GitHub account. Opens a new window/tab).