From 6dba4da3ed7521ac81b88ea5c7efece12f7a25d1 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 29 Aug 2013 16:47:48 -0400 Subject: [PATCH 1/8] Fixes #828: refactored QueryBuilder::build() --- framework/yii/db/ActiveQuery.php | 8 ++++---- framework/yii/db/ActiveRelation.php | 4 ++-- framework/yii/db/Query.php | 4 ++-- framework/yii/db/QueryBuilder.php | 20 ++++++++++++-------- tests/unit/framework/db/QueryBuilderTest.php | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/framework/yii/db/ActiveQuery.php b/framework/yii/db/ActiveQuery.php index 4d08659..12997ee 100644 --- a/framework/yii/db/ActiveQuery.php +++ b/framework/yii/db/ActiveQuery.php @@ -156,6 +156,8 @@ class ActiveQuery extends Query if ($db === null) { $db = $modelClass::getDb(); } + + $params = $this->params; if ($this->sql === null) { if ($this->from === null) { $tableName = $modelClass::tableName(); @@ -164,11 +166,9 @@ class ActiveQuery extends Query } $this->from = array($tableName); } - /** @var $qb QueryBuilder */ - $qb = $db->getQueryBuilder(); - $this->sql = $qb->build($this); + list ($this->sql, $params) = $db->getQueryBuilder()->build($this); } - return $db->createCommand($this->sql, $this->params); + return $db->createCommand($this->sql, $params); } /** diff --git a/framework/yii/db/ActiveRelation.php b/framework/yii/db/ActiveRelation.php index f10f287..0eee25b 100644 --- a/framework/yii/db/ActiveRelation.php +++ b/framework/yii/db/ActiveRelation.php @@ -297,7 +297,7 @@ class ActiveRelation extends ActiveQuery /** @var $primaryModel ActiveRecord */ $primaryModel = reset($primaryModels); $db = $primaryModel->getDb(); - $sql = $db->getQueryBuilder()->build($this); - return $db->createCommand($sql, $this->params)->queryAll(); + list ($sql, $params) = $db->getQueryBuilder()->build($this); + return $db->createCommand($sql, $params)->queryAll(); } } diff --git a/framework/yii/db/Query.php b/framework/yii/db/Query.php index 19ea028..d1e7864 100644 --- a/framework/yii/db/Query.php +++ b/framework/yii/db/Query.php @@ -149,8 +149,8 @@ class Query extends Component if ($db === null) { $db = Yii::$app->getDb(); } - $sql = $db->getQueryBuilder()->build($this); - return $db->createCommand($sql, $this->params); + list ($sql, $params) = $db->getQueryBuilder()->build($this); + return $db->createCommand($sql, $params); } /** diff --git a/framework/yii/db/QueryBuilder.php b/framework/yii/db/QueryBuilder.php index b55be3c..9a9329d 100644 --- a/framework/yii/db/QueryBuilder.php +++ b/framework/yii/db/QueryBuilder.php @@ -55,22 +55,24 @@ class QueryBuilder extends \yii\base\Object /** * Generates a SELECT SQL statement from a [[Query]] object. * @param Query $query the [[Query]] object from which the SQL statement will be generated - * @return string the generated SQL statement + * @return array the generated SQL statement (the first array element) and the corresponding + * parameters to be bound to the SQL statement (the second array element). */ public function build($query) { + $params = $query->params; $clauses = array( $this->buildSelect($query->select, $query->distinct, $query->selectOption), $this->buildFrom($query->from), - $this->buildJoin($query->join, $query->params), - $this->buildWhere($query->where, $query->params), + $this->buildJoin($query->join, $params), + $this->buildWhere($query->where, $params), $this->buildGroupBy($query->groupBy), - $this->buildHaving($query->having, $query->params), - $this->buildUnion($query->union, $query->params), + $this->buildHaving($query->having, $params), + $this->buildUnion($query->union, $params), $this->buildOrderBy($query->orderBy), $this->buildLimit($query->limit, $query->offset), ); - return implode($this->separator, array_filter($clauses)); + return array(implode($this->separator, array_filter($clauses)), $params); } /** @@ -718,9 +720,11 @@ class QueryBuilder extends \yii\base\Object } 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 + $originalParams = $union->params; $union->addParams($params); - $unions[$i] = $this->build($union); - $params = $union->params; + list ($unions[$i], $params) = $this->build($union); + $union->params = $originalParams; } } return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)"; diff --git a/tests/unit/framework/db/QueryBuilderTest.php b/tests/unit/framework/db/QueryBuilderTest.php index 146cfdb..e08ac87 100644 --- a/tests/unit/framework/db/QueryBuilderTest.php +++ b/tests/unit/framework/db/QueryBuilderTest.php @@ -112,7 +112,7 @@ class QueryBuilderTest extends DatabaseTestCase } } - public function testAddDropPrimayKey() + public function testAddDropPrimaryKey() { $tableName = 'tbl_constraints'; $pkeyName = $tableName . "_pkey"; From 30d622994369923f0a07c36fc449cf440ca92607 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 29 Aug 2013 16:53:15 -0400 Subject: [PATCH 2/8] Fixed test break. --- tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php b/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php index 2f6b164..3291187 100644 --- a/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php +++ b/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php @@ -73,9 +73,9 @@ class SqliteQueryBuilderTest extends QueryBuilderTest ); } - public function testAddDropPrimayKey() + public function testAddDropPrimaryKey() { $this->setExpectedException('yii\base\NotSupportedException'); - parent::testAddDropPrimayKey(); + parent::testAddDropPrimaryKey(); } } From cb60fa5ddd4b3b76d4c55cb80a227f98bc983913 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 29 Aug 2013 17:19:22 -0400 Subject: [PATCH 3/8] Make sure query cloning works as expected. --- framework/yii/data/ActiveDataProvider.php | 1 - framework/yii/db/ActiveRelation.php | 10 ++++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/framework/yii/data/ActiveDataProvider.php b/framework/yii/data/ActiveDataProvider.php index e36ef90..aaf71b2 100644 --- a/framework/yii/data/ActiveDataProvider.php +++ b/framework/yii/data/ActiveDataProvider.php @@ -9,7 +9,6 @@ namespace yii\data; use Yii; use yii\base\InvalidConfigException; -use yii\base\InvalidParamException; use yii\base\Model; use yii\db\Query; use yii\db\ActiveQuery; diff --git a/framework/yii/db/ActiveRelation.php b/framework/yii/db/ActiveRelation.php index 0eee25b..0be4feb 100644 --- a/framework/yii/db/ActiveRelation.php +++ b/framework/yii/db/ActiveRelation.php @@ -50,6 +50,16 @@ class ActiveRelation extends ActiveQuery */ public $via; + /** + * Clones internal objects. + */ + public function __clone() + { + if (is_object($this->via)) { + // make a clone of "via" object so that the same query object can be reused multiple times + $this->via = clone $this->via; + } + } /** * Specifies the relation associated with the pivot table. From 2ecdfd62252ebb682b2edadf1ed42635df9143e0 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 29 Aug 2013 17:20:04 -0400 Subject: [PATCH 4/8] gii form width adjustment. --- framework/yii/gii/views/default/view.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/gii/views/default/view.php b/framework/yii/gii/views/default/view.php index 821f6fc..1782c77 100644 --- a/framework/yii/gii/views/default/view.php +++ b/framework/yii/gii/views/default/view.php @@ -30,7 +30,7 @@ foreach ($generator->templates as $name => $path) { array('class' => ActiveField::className()))); ?>
-
+
renderFile($generator->formView(), array( 'generator' => $generator, 'form' => $form, From c2c12a9049ec321e77ee13bc7f8a9a8293596b21 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 30 Aug 2013 13:44:22 +0400 Subject: [PATCH 5/8] Fixes #829: loginRequired now responds with HTTP 403 in case of AJAX or loginUrl is not set --- framework/yii/web/User.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/framework/yii/web/User.php b/framework/yii/web/User.php index b1ca8c2..4784063 100644 --- a/framework/yii/web/User.php +++ b/framework/yii/web/User.php @@ -287,10 +287,8 @@ class User extends Component public function loginRequired() { $request = Yii::$app->getRequest(); - if (!$request->getIsAjax()) { + if ($this->loginUrl !== null && !$request->getIsAjax()) { $this->setReturnUrl($request->getUrl()); - } - if ($this->loginUrl !== null) { Yii::$app->getResponse()->redirect($this->loginUrl)->send(); exit(); } else { From 70d263d66ebfc254d48296a0eed26a73722e163c Mon Sep 17 00:00:00 2001 From: Philipp Frenzel Date: Fri, 30 Aug 2013 12:23:10 +0200 Subject: [PATCH 6/8] Update module.php module should extend \yii\base\Module as \yii\web\Module doesn't exist... If this is outdated, then pls just ignore it! --- framework/yii/gii/generators/module/templates/module.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/gii/generators/module/templates/module.php b/framework/yii/gii/generators/module/templates/module.php index 911cb29..40af635 100644 --- a/framework/yii/gii/generators/module/templates/module.php +++ b/framework/yii/gii/generators/module/templates/module.php @@ -16,7 +16,7 @@ echo "; -class extends \yii\web\Module +class extends \yii\base\Module { public $controllerNamespace = 'getControllerNamespace(); ?>'; From e94b4555355479913f96d475d8292dc02e72fdff Mon Sep 17 00:00:00 2001 From: Philipp Frenzel Date: Fri, 30 Aug 2013 15:05:00 +0200 Subject: [PATCH 7/8] add missing return to module template I think according to the framework it should return the render and otherwise the browser will stay blank... --- framework/yii/gii/generators/module/templates/controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/gii/generators/module/templates/controller.php b/framework/yii/gii/generators/module/templates/controller.php index 34dd961..4d3da93 100644 --- a/framework/yii/gii/generators/module/templates/controller.php +++ b/framework/yii/gii/generators/module/templates/controller.php @@ -16,6 +16,6 @@ class DefaultController extends Controller { public function actionIndex() { - $this->render('index'); + return $this->render('index'); } } From e1a6aacba8ac4b19c95d716a74f1da5db35d5ca7 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 30 Aug 2013 18:22:53 +0400 Subject: [PATCH 8/8] Fixes #829: loginRequired now responds with HTTP 403 in case of AJAX or loginUrl is not set (reverted from commit c2c12a9049ec321e77ee13bc7f8a9a8293596b21) --- framework/yii/web/User.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/yii/web/User.php b/framework/yii/web/User.php index 4784063..b1ca8c2 100644 --- a/framework/yii/web/User.php +++ b/framework/yii/web/User.php @@ -287,8 +287,10 @@ class User extends Component public function loginRequired() { $request = Yii::$app->getRequest(); - if ($this->loginUrl !== null && !$request->getIsAjax()) { + if (!$request->getIsAjax()) { $this->setReturnUrl($request->getUrl()); + } + if ($this->loginUrl !== null) { Yii::$app->getResponse()->redirect($this->loginUrl)->send(); exit(); } else {