diff options
-rw-r--r-- | includes/database/select.inc | 24 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.test | 14 |
2 files changed, 33 insertions, 5 deletions
diff --git a/includes/database/select.inc b/includes/database/select.inc index 3e7d27762..ddb78c69d 100644 --- a/includes/database/select.inc +++ b/includes/database/select.inc @@ -232,7 +232,9 @@ interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableIn * this clause should use a named placeholder and the value or values to * insert should be passed in the 4th parameter. For the first table joined * on a query, this value is ignored as the first table is taken as the base - * table. + * table. The token %alias can be used in this string to be replaced with + * the actual alias. This is useful when $alias is modified by the database + * system, for example, when joining the same table more than once. * @param $arguments * An array of arguments to replace into the $condition of this join. * @return @@ -253,7 +255,9 @@ interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableIn * this clause should use a named placeholder and the value or values to * insert should be passed in the 4th parameter. For the first table joined * on a query, this value is ignored as the first table is taken as the base - * table. + * table. The token %alias can be used in this string to be replaced with + * the actual alias. This is useful when $alias is modified by the database + * system, for example, when joining the same table more than once. * @param $arguments * An array of arguments to replace into the $condition of this join. * @return @@ -274,7 +278,9 @@ interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableIn * this clause should use a named placeholder and the value or values to * insert should be passed in the 4th parameter. For the first table joined * on a query, this value is ignored as the first table is taken as the base - * table. + * table. The token %alias can be used in this string to be replaced with + * the actual alias. This is useful when $alias is modified by the database + * system, for example, when joining the same table more than once. * @param $arguments * An array of arguments to replace into the $condition of this join. * @return @@ -295,7 +301,9 @@ interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableIn * this clause should use a named placeholder and the value or values to * insert should be passed in the 4th parameter. For the first table joined * on a query, this value is ignored as the first table is taken as the base - * table. + * table. The token %alias can be used in this string to be replaced with + * the actual alias. This is useful when $alias is modified by the database + * system, for example, when joining the same table more than once. * @param $arguments * An array of arguments to replace into the $condition of this join. * @return @@ -324,7 +332,9 @@ interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableIn * this clause should use a named placeholder and the value or values to * insert should be passed in the 4th parameter. For the first table joined * on a query, this value is ignored as the first table is taken as the base - * table. + * table. The token %alias can be used in this string to be replaced with + * the actual alias. This is useful when $alias is modified by the database + * system, for example, when joining the same table more than once. * @param $arguments * An array of arguments to replace into the $condition of this join. * @return @@ -1210,6 +1220,10 @@ class SelectQuery extends Query implements SelectQueryInterface { } $alias = $alias_candidate; + if (is_string($condition)) { + $condition = str_replace('%alias', $alias, $condition); + } + $this->tables[$alias] = array( 'join type' => $type, 'table' => $table, diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index 064ed3890..dd3f41850 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1972,6 +1972,20 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase { $job = $query->execute()->fetchField(); $this->assertEqual($job, 'Songwriter', t('Correct data retrieved.')); } + + /** + * Confirm we can join on a single table twice with a dynamic alias. + */ + function testJoinTwice() { + $query = db_select('test')->fields('test'); + $alias = $query->join('test', 'test', 'test.job = %alias.job'); + $query->addField($alias, 'name', 'othername'); + $query->addField($alias, 'job', 'otherjob'); + $query->where("$alias.name <> test.name"); + $crowded_job = $query->execute()->fetch(); + $this->assertEqual($crowded_job->job, $crowded_job->otherjob, t('Correctly joined same table twice.')); + $this->assertNotEqual($crowded_job->name, $crowded_job->othername, t('Correctly joined same table twice.')); + } } class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase { |