From 12cbaf90b5c622d4502aeafb73bb4ef3bf373f8e Mon Sep 17 00:00:00 2001 From: Ivan Pomortsev Date: Thu, 23 Jan 2014 07:47:48 +0300 Subject: [PATCH] Update QueryBuilder.php Change using of implode to convert array of queries to union to array_reduce. Result of this changing is that code feel difference between two selects that connects with UNION and two selects that connects with UNION ALL. --- framework/db/QueryBuilder.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/framework/db/QueryBuilder.php b/framework/db/QueryBuilder.php index 7cadc1d6fc..5193769f96 100644 --- a/framework/db/QueryBuilder.php +++ b/framework/db/QueryBuilder.php @@ -739,6 +739,20 @@ class QueryBuilder extends \yii\base\Object if (empty($unions)) { return ''; } + + /** + * @param string $left left element of reducing pair + * @param string $right right element of reducing pair + * @return string imploding pair with "UNION ALL" if + * right element is array and "UNION" if not + */ + $reducer = function($left, $right) + { + if($all = is_array($right)) + list ($right) = $right; + return $left . ' UNION ' . ($all ? 'ALL ' : ' ') . $right . ' ) '; + } + foreach ($unions as $i => $union) { if ($union instanceof Query) { // save the original parameters so that we can restore them later to prevent from modifying the query object @@ -748,7 +762,7 @@ class QueryBuilder extends \yii\base\Object $union->params = $originalParams; } } - return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)"; + return array_reduce($unions, $reducer); } /**