->selectAs() – Build the select component of a query (usually for joins)


void $DB_DataObject->selectAs ( object | array $columns_or_object , string $format , string $table_name )


Auto creates select items based on either the current objects column names, the supplied object, or an array.

This is primarily used in conjunciton with joinAdd, to enable the renaming of columns into a fixed format when they are likely to have naming conflicts (like both tables have an 'id' column).

Sending no arguments to selectAs, will reset the current select (usually removing the default *), and build a select list based on the current objects column names.


  • object | array $column_or_object - a dataobject or array of column names

  • string $format - the format the columns will appear, using sprintf format, the %s is replaced with the column name so car_%s would result in the SQL 'car.name as car_name' being generated.

  • string $tableName - this is used either when use send an array as the first argument, or when you are joining a table 'as' another name,


This function can not be called statically.


Using selectAs()

// ok lets see what is going on..
$person = new DataObjects_Person;

// to generate "person.id as id , person.name as name ......."

// to generate a restricted list.. "person.age as age , person.name as name"

// using another object. 
$car = new DataObjects_Car;

// this is the first car (
$car->use 'first';

// using the joinadd add the car..

// now add all the select columns for the car eg. "car.id as car_id, car.name as car_name ...."

// select only a few columns from the car table.
// note you have to use the table name at the end..
$person->selectAs(array('color','topspeed'), 'car_%s','car');

// now the user can have a second car....
$secondcar = new DataObjects_Car;
$secondcar->use 'second';

// now since we alreay have one car, the sql statement would get messy 
// so we are joining this as the second car "FROM person INNER JOIN car ..... , car as secondcar WHERE .....

// and we can now add all the columns
// "secondcar.id as secondcar_id, secondcar.name as secondcar_name ........
// note that you must use the last field as the SECONDCAR.ID format uses the 'AS' name, rather than the 
// objects real table name 'car'


// ok fire of a query...
while (
$person->fetch()) {

load related objects (Previous) add another dataobject to build a create join query (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: user@example.com
To have columns prefixed for primary table :
$do->selectAs(null, 'prefix_%s');