Commit a4065554 authored by webchick's avatar webchick

#318440 by Crell: Make it easier to add multiple fields in a dynamic SELECT statement.

parent a7481a7c
......@@ -37,6 +37,7 @@ class SelectQuery extends Query implements QueryConditionInterface, QueryAlterab
* 'alias' => $alias_of_the_table,
* 'condition' => $condition_clause_on_which_to_join,
* 'arguments' => $array_of_arguments_for_placeholders_in_the condition.
* 'all_fields' => TRUE to SELECT $alias.*, FALSE or NULL otherwise.
* )
*
* @var array
......@@ -362,6 +363,43 @@ public function addField($table_alias, $field, $alias = NULL) {
return $alias;
}
/**
* Add multiple fields from the same table to be SELECTed.
*
* This method does not return the aliases set for the passed fields. In the
* majority of cases that is not a problem, as the alias will be the field
* name. However, if you do need to know the alias you can call getFields()
* and examine the result to determine what alias was created. Alternatively,
* simply use addField() for the few fields you care about and this method for
* the rest.
*
* @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. If not specified, $table_alias.* will be generated
* without any aliases.
* @return
* The called object.
*/
public function fields($table_alias, Array $fields = array()) {
if ($fields) {
foreach ($fields as $field) {
// We don't care what alias was assigned.
$this->addField($table_alias, $field);
}
}
else {
// We want all fields from this table.
$this->tables[$table_alias]['all_fields'] = TRUE;
}
return $this;
}
/**
* Adds an expression to the list of "fields" to be SELECTed.
*
......@@ -640,8 +678,14 @@ public function __toString() {
foreach ($this->expressions as $alias => $expression) {
$fields[] = $expression['expression'] . ' AS ' . $expression['alias'];
}
foreach ($this->tables as $alias => $table) {
if (!empty($table['all_fields'])) {
$fields[] = $alias . '.*';
}
}
$query .= implode(', ', $fields);
// FROM - We presume all queries have a FROM, as any query that doesn't won't need the query builder anyway.
$query .= "\nFROM ";
foreach ($this->tables as $alias => $table) {
......
......@@ -1042,6 +1042,54 @@ class DatabaseSelectTestCase extends DatabaseTestCase {
$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() {
$record = db_select('test')
->fields('test', array('id', 'name', 'age', 'job'))
->condition('age', 27)
->execute()->fetchObject();
// Check that all fields we asked for are present.
$this->assertNotNull($record->id, t('ID field is present.'));
$this->assertNotNull($record->name, t('Name field is present.'));
$this->assertNotNull($record->age, t('Age field is present.'));
$this->assertNotNull($record->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->id, 2, t('ID field has the correct value.'));
$this->assertEqual($record->name, 'George', t('Name field has the correct value.'));
$this->assertEqual($record->age, 27, t('Age field has the correct value.'));
$this->assertEqual($record->job, 'Singer', t('Job field has the correct value.'));
}
/**
* Test adding all fields from a given table to a select statement.
*/
function testSimpleSelectAllFields() {
$record = db_select('test')
->fields('test')
->condition('age', 27)
->execute()->fetchObject();
// Check that all fields we asked for are present.
$this->assertNotNull($record->id, t('ID field is present.'));
$this->assertNotNull($record->name, t('Name field is present.'));
$this->assertNotNull($record->age, t('Age field is present.'));
$this->assertNotNull($record->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->id, 2, t('ID field has the correct value.'));
$this->assertEqual($record->name, 'George', t('Name field has the correct value.'));
$this->assertEqual($record->age, 27, t('Age field has the correct value.'));
$this->assertEqual($record->job, 'Singer', t('Job field has the correct value.'));
}
}
/**
......@@ -1119,7 +1167,6 @@ class DatabaseSelectOrderedTestCase extends DatabaseTestCase {
}
}
/**
* Test order by descending.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment