@@ -883,14 +883,18 @@ class SelectQuery extends Query implements SelectQueryInterface {
883
883
* 'type' => $join_type (one of INNER, LEFT OUTER, RIGHT OUTER),
884
884
* 'table' => $table,
885
885
* 'alias' => $alias_of_the_table,
886
- * 'condition' => $condition_clause_on_which_to_join ,
886
+ * 'condition' => $join_condition (string or Condition object) ,
887
887
* 'arguments' => $array_of_arguments_for_placeholders_in_the condition.
888
888
* 'all_fields' => TRUE to SELECT $alias.*, FALSE or NULL otherwise.
889
889
* )
890
890
*
891
891
* If $table is a string, it is taken as the name of a table. If it is
892
892
* a SelectQuery object, it is taken as a subquery.
893
893
*
894
+ * If $join_condition is a Condition object, any arguments should be
895
+ * incorporated into the object; a separate array of arguments does not need
896
+ * to be provided.
897
+ *
894
898
* @var array
895
899
*/
896
900
protected $ tables = array ();
@@ -1028,6 +1032,10 @@ class SelectQuery extends Query implements SelectQueryInterface {
1028
1032
if ($ table ['table ' ] instanceof SelectQueryInterface) {
1029
1033
$ args += $ table ['table ' ]->arguments ();
1030
1034
}
1035
+ // If the join condition is an object, grab its arguments recursively.
1036
+ if (!empty ($ table ['condition ' ]) && $ table ['condition ' ] instanceof QueryConditionInterface) {
1037
+ $ args += $ table ['condition ' ]->arguments ();
1038
+ }
1031
1039
}
1032
1040
1033
1041
foreach ($ this ->expressions as $ expression ) {
@@ -1079,6 +1087,10 @@ class SelectQuery extends Query implements SelectQueryInterface {
1079
1087
if ($ table ['table ' ] instanceof SelectQueryInterface) {
1080
1088
$ table ['table ' ]->compile ($ connection , $ queryPlaceholder );
1081
1089
}
1090
+ // Make sure join conditions are also compiled.
1091
+ if (!empty ($ table ['condition ' ]) && $ table ['condition ' ] instanceof QueryConditionInterface) {
1092
+ $ table ['condition ' ]->compile ($ connection , $ queryPlaceholder );
1093
+ }
1082
1094
}
1083
1095
1084
1096
// If there are any dependent queries to UNION, compile it recursively.
@@ -1099,6 +1111,11 @@ class SelectQuery extends Query implements SelectQueryInterface {
1099
1111
return FALSE ;
1100
1112
}
1101
1113
}
1114
+ if (!empty ($ table ['condition ' ]) && $ table ['condition ' ] instanceof QueryConditionInterface) {
1115
+ if (!$ table ['condition ' ]->compiled ()) {
1116
+ return FALSE ;
1117
+ }
1118
+ }
1102
1119
}
1103
1120
1104
1121
foreach ($ this ->union as $ union ) {
@@ -1568,7 +1585,7 @@ class SelectQuery extends Query implements SelectQueryInterface {
1568
1585
$ query .= $ table_string . ' ' . $ this ->connection ->escapeAlias ($ table ['alias ' ]);
1569
1586
1570
1587
if (!empty ($ table ['condition ' ])) {
1571
- $ query .= ' ON ' . $ table ['condition ' ];
1588
+ $ query .= ' ON ' . ( string ) $ table ['condition ' ];
1572
1589
}
1573
1590
}
1574
1591
@@ -1589,6 +1606,14 @@ class SelectQuery extends Query implements SelectQueryInterface {
1589
1606
$ query .= "\nHAVING " . $ this ->having ;
1590
1607
}
1591
1608
1609
+ // UNION is a little odd, as the select queries to combine are passed into
1610
+ // this query, but syntactically they all end up on the same level.
1611
+ if ($ this ->union ) {
1612
+ foreach ($ this ->union as $ union ) {
1613
+ $ query .= ' ' . $ union ['type ' ] . ' ' . (string ) $ union ['query ' ];
1614
+ }
1615
+ }
1616
+
1592
1617
// ORDER BY
1593
1618
if ($ this ->order ) {
1594
1619
$ query .= "\nORDER BY " ;
@@ -1608,14 +1633,6 @@ class SelectQuery extends Query implements SelectQueryInterface {
1608
1633
$ query .= "\nLIMIT " . (int ) $ this ->range ['length ' ] . " OFFSET " . (int ) $ this ->range ['start ' ];
1609
1634
}
1610
1635
1611
- // UNION is a little odd, as the select queries to combine are passed into
1612
- // this query, but syntactically they all end up on the same level.
1613
- if ($ this ->union ) {
1614
- foreach ($ this ->union as $ union ) {
1615
- $ query .= ' ' . $ union ['type ' ] . ' ' . (string ) $ union ['query ' ];
1616
- }
1617
- }
1618
-
1619
1636
if ($ this ->forUpdate ) {
1620
1637
$ query .= ' FOR UPDATE ' ;
1621
1638
}
@@ -1624,6 +1641,8 @@ class SelectQuery extends Query implements SelectQueryInterface {
1624
1641
}
1625
1642
1626
1643
public function __clone () {
1644
+ parent ::__clone ();
1645
+
1627
1646
// On cloning, also clone the dependent objects. However, we do not
1628
1647
// want to clone the database connection object as that would duplicate the
1629
1648
// connection itself.
@@ -1633,6 +1652,11 @@ class SelectQuery extends Query implements SelectQueryInterface {
1633
1652
foreach ($ this ->union as $ key => $ aggregate ) {
1634
1653
$ this ->union [$ key ]['query ' ] = clone ($ aggregate ['query ' ]);
1635
1654
}
1655
+ foreach ($ this ->tables as $ alias => $ table ) {
1656
+ if ($ table ['table ' ] instanceof SelectQueryInterface) {
1657
+ $ this ->tables [$ alias ]['table ' ] = clone $ table ['table ' ];
1658
+ }
1659
+ }
1636
1660
}
1637
1661
}
1638
1662
0 commit comments