Index: includes/database/select.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/database/select.inc,v retrieving revision 1.4 diff -u -p -r1.4 select.inc --- includes/database/select.inc 15 Sep 2008 05:00:48 -0000 1.4 +++ includes/database/select.inc 10 Oct 2008 04:49:35 -0000 @@ -356,6 +356,30 @@ class SelectQuery extends Query implemen } /** + * Add multiple fields from the same table to be SELECTed. + * + * @param $table_alias + * The name of the table from which the field comes, as an alias. Generally + * you will want to use the return value of join() here to ensure that it is + * valid. + * @param $fields + * An indexed array of fields present in the specified table that should be + * included in this query. + * @return + * An associative array of fields (those specified in $fields) mapped to + * the alias they were assigned. + */ + public function addFields($table_alias, Array $fields) { + $aliases = array(); + + foreach ($fields as $field) { + $aliases[$field] = $this->addField($table_alias, $field); + } + + return $aliases; + } + + /** * Adds an expression to the list of "fields" to be SELECTed. * * An expression can be any arbitrary string that is valid SQL. That includes Index: modules/simpletest/tests/database_test.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v retrieving revision 1.9 diff -u -p -r1.9 database_test.test --- modules/simpletest/tests/database_test.test 28 Sep 2008 21:52:08 -0000 1.9 +++ modules/simpletest/tests/database_test.test 10 Oct 2008 04:49:35 -0000 @@ -1011,6 +1011,29 @@ class DatabaseSelectTestCase extends Dat $this->assertEqual($record->$name_field, 'George', t('Fetched name is correct.')); $this->assertEqual($record->$age_field, 27*2, t('Fetched age expression is correct.')); } + + /** + * Test adding multiple fields to a select statement at the same time. + */ + function testSimpleSelectMultipleFields() { + $query = db_select('test'); + $aliases = $query->addFields('test', array('id', 'name', 'age', 'job')); + $query->condition('age', 27); + $record = $query->execute()->fetchObject(); + + // Check that all fields we asked for are present. + $this->assertNotNull($record->$aliases['id'], t('ID field is present.')); + $this->assertNotNull($record->$aliases['name'], t('Name field is present.')); + $this->assertNotNull($record->$aliases['age'], t('Age field is present.')); + $this->assertNotNull($record->$aliases['job'], t('Job field is present.')); + + // Ensure that we got the right record. + // Check that all fields we asked for are present. + $this->assertEqual($record->$aliases['id'], 2, t('ID field has the correct value.')); + $this->assertEqual($record->$aliases['name'], 'George', t('Name field has the correct value.')); + $this->assertEqual($record->$aliases['age'], 27, t('Age field has the correct value.')); + $this->assertEqual($record->$aliases['job'], 'Singer', t('Job field has the correct value.')); + } } /** @@ -1088,7 +1111,6 @@ class DatabaseSelectOrderedTestCase exte } } - /** * Test order by descending. */