Commit a9286b89 by Ivan Pomortsev

Update QueryBuilder.php

Change <code>array_reduce</code> with anonimous function variant to easy string collection from pieces of unions.
parent c9c59bdc
...@@ -740,35 +740,22 @@ class QueryBuilder extends \yii\base\Object ...@@ -740,35 +740,22 @@ class QueryBuilder extends \yii\base\Object
return ''; return '';
} }
/** $result = '';
* @param Query $left left element of reducing pair
* @param Query $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(is_array($left))
$left = $left['query'];
$all = false;
if(is_array($right)) {
$all = $right['all'];
$right = $right['query'];
}
return $left . ' UNION ' . ($all ? 'ALL ' : '') . '( ' . $right . ' )';
};
foreach ($unions as $i => $union) { foreach ($unions as $i => $union) {
if ($union instanceof Query) { $query = $union['query'];
if ($query instanceof Query) {
// save the original parameters so that we can restore them later to prevent from modifying the query object // save the original parameters so that we can restore them later to prevent from modifying the query object
$originalParams = $union->params; $originalParams = $query->params;
$union->addParams($params); $query->addParams($params);
list ($unions[$i]['query'], $params) = $this->build($query); list ($unions[$i]['query'], $params) = $this->build($query);
$union->params = $originalParams; $query->params = $originalParams;
} }
$result .= 'UNION ' . ($union['all'] ? 'ALL ' : '') . '( ' . $unions[$i]['query'] . ' ) ';
} }
return trim(array_reduce($unions, $reducer)); return trim($result);
} }
/** /**
......
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