From 50743935d3a215701cd17b08910fb6d42e8eae2a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 1 Mar 2012 20:18:54 -0500 Subject: [PATCH] ... --- framework/db/ar/ActiveFinder.php | 6 +- framework/db/ar/ActiveQuery.php | 71 +--------------------- framework/db/ar/ActiveRecord.php | 7 ++- framework/db/ar/ActiveRelation.php | 29 +-------- framework/db/ar/BaseActiveQuery.php | 81 +++++++++++++++++++++++++ tests/unit/framework/db/ar/ActiveRecordTest.php | 11 ++++ 6 files changed, 99 insertions(+), 106 deletions(-) create mode 100644 framework/db/ar/BaseActiveQuery.php diff --git a/framework/db/ar/ActiveFinder.php b/framework/db/ar/ActiveFinder.php index 5db34e1..080ea53 100644 --- a/framework/db/ar/ActiveFinder.php +++ b/framework/db/ar/ActiveFinder.php @@ -68,7 +68,6 @@ class ActiveFinder extends \yii\base\Object } else { $this->initFrom($query); $this->applyScopes($query); - // todo: filter $sql = $this->connection->getQueryBuilder()->build($query); if (strpos($sql, '@.') !== false) { if ($query->tableAlias !== null) { @@ -189,10 +188,7 @@ class ActiveFinder extends \yii\base\Object { if (is_array($query->scopes)) { $class = $query->modelClass; - $defaultScope = $class::defaultScope(); - if ($defaultScope !== null) { - call_user_func_array($defaultScope, array($query)); - } + $class::defaultScope($query); $scopes = $class::scopes(); foreach ($query->scopes as $name => $params) { if (is_integer($name)) { diff --git a/framework/db/ar/ActiveQuery.php b/framework/db/ar/ActiveQuery.php index b673f21..ffb76be 100644 --- a/framework/db/ar/ActiveQuery.php +++ b/framework/db/ar/ActiveQuery.php @@ -11,7 +11,6 @@ namespace yii\db\ar; use yii\base\VectorIterator; -use yii\db\dao\BaseQuery; use yii\db\dao\Expression; use yii\db\Exception; @@ -39,39 +38,9 @@ use yii\db\Exception; * @author Qiang Xue * @since 2.0 */ -class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess, \Countable +class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayAccess, \Countable { /** - * @var string the name of the ActiveRecord class. - */ - public $modelClass; - /** - * @var array list of relations that this query should be performed with - */ - public $with; - /** - * @var array list of relations that should be used as filters for this query. - */ - public $filters; - /** - * @var string the table alias to be used for query - */ - public $tableAlias; - /** - * @var string the name of the column that the result should be indexed by. - * This is only useful when the query result is returned as an array. - */ - public $indexBy; - /** - * @var boolean whether to return each record as an array. If false (default), an object - * of [[modelClass]] will be created to represent each record. - */ - public $asArray; - /** - * @var array list of scopes that should be applied to this query - */ - public $scopes; - /** * @var string the SQL statement to be executed to retrieve primary records. * This is set by [[ActiveRecord::findBySql()]]. */ @@ -101,44 +70,6 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess, } } - public function asArray($value = true) - { - $this->asArray = $value; - return $this; - } - - public function with() - { - $this->with = func_get_args(); - if (isset($this->with[0]) && is_array($this->with[0])) { - // the parameter is given as an array - $this->with = $this->with[0]; - } - return $this; - } - - public function filters() - { - $this->filters = func_get_args(); - if (isset($this->filters[0]) && is_array($this->filters[0])) { - // the parameter is given as an array - $this->filters = $this->filters[0]; - } - return $this; - } - - public function indexBy($column) - { - $this->indexBy = $column; - return $this; - } - - public function tableAlias($value) - { - $this->tableAlias = $value; - return $this; - } - /** * Executes query and returns all results as an array. * @return array the query results. If the query results in nothing, an empty array will be returned. diff --git a/framework/db/ar/ActiveRecord.php b/framework/db/ar/ActiveRecord.php index cc41292..b665259 100644 --- a/framework/db/ar/ActiveRecord.php +++ b/framework/db/ar/ActiveRecord.php @@ -364,12 +364,13 @@ abstract class ActiveRecord extends Model * Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. * The default implementation simply returns an empty array. You may override this method * if the model needs to be queried with some default criteria (e.g. only active records should be returned). - * @return array the query criteria. This will be used as the parameter to the constructor + * @param BaseActiveQuery + * @return BaseActiveQuery the query criteria. This will be used as the parameter to the constructor * of {@link CDbCriteria}. */ - public static function defaultScope() + public static function defaultScope($query) { - return array(); + return $query; } /** diff --git a/framework/db/ar/ActiveRelation.php b/framework/db/ar/ActiveRelation.php index f0a7602..a92b509 100644 --- a/framework/db/ar/ActiveRelation.php +++ b/framework/db/ar/ActiveRelation.php @@ -10,25 +10,19 @@ namespace yii\db\ar; -use yii\db\dao\BaseQuery; - /** * ActiveRelation represents the specification of a relation declared in [[ActiveRecord::relations()]]. * * @author Qiang Xue * @since 2.0 */ -class ActiveRelation extends BaseQuery +class ActiveRelation extends BaseActiveQuery { /** * @var string the name of this relation */ public $name; /** - * @var string the name of the model class that this relation represents - */ - public $modelClass; - /** * @var boolean whether this relation is a one-many relation */ public $hasMany; @@ -38,15 +32,6 @@ class ActiveRelation extends BaseQuery */ public $joinType; /** - * @var string the table alias used for the corresponding table during query - */ - public $tableAlias; - /** - * @var string the name of the column that the result should be indexed by. - * This is only useful when [[hasMany]] is true. - */ - public $indexBy; - /** * @var string the ON clause of the join query */ public $on; @@ -54,16 +39,4 @@ class ActiveRelation extends BaseQuery * @var string */ public $via; - /** - * @var array the relations that should be queried together (eager loading) - */ - public $with; - /** - * @var array the relations that should be used as filters for this query - */ - public $filters; - /** - * @var array the scopes that should be applied during query - */ - public $scopes; } diff --git a/framework/db/ar/BaseActiveQuery.php b/framework/db/ar/BaseActiveQuery.php new file mode 100644 index 0000000..c53a4f2 --- /dev/null +++ b/framework/db/ar/BaseActiveQuery.php @@ -0,0 +1,81 @@ + + * @link http://www.yiiframework.com/ + * @copyright Copyright © 2008-2012 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +namespace yii\db\ar; + +use yii\db\dao\BaseQuery; + +/** + * @author Qiang Xue + * @since 2.0 + */ +class BaseActiveQuery extends BaseQuery +{ + /** + * @var string the name of the ActiveRecord class. + */ + public $modelClass; + /** + * @var array list of relations that this query should be performed with + */ + public $with; + /** + * @var string the table alias to be used for query + */ + public $tableAlias; + /** + * @var string the name of the column that the result should be indexed by. + * This is only useful when the query result is returned as an array. + */ + public $indexBy; + /** + * @var boolean whether to return each record as an array. If false (default), an object + * of [[modelClass]] will be created to represent each record. + */ + public $asArray; + /** + * @var array list of scopes that should be applied to this query + */ + public $scopes; + + public function asArray($value = true) + { + $this->asArray = $value; + return $this; + } + + public function with() + { + $this->with = func_get_args(); + if (isset($this->with[0]) && is_array($this->with[0])) { + // the parameter is given as an array + $this->with = $this->with[0]; + } + return $this; + } + + public function indexBy($column) + { + $this->indexBy = $column; + return $this; + } + + public function tableAlias($value) + { + $this->tableAlias = $value; + return $this; + } + + public function scopes($names) + { + $this->scopes = $names; + return $this; + } +} diff --git a/tests/unit/framework/db/ar/ActiveRecordTest.php b/tests/unit/framework/db/ar/ActiveRecordTest.php index b851309..21e1ec7 100644 --- a/tests/unit/framework/db/ar/ActiveRecordTest.php +++ b/tests/unit/framework/db/ar/ActiveRecordTest.php @@ -208,6 +208,17 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase // scopes $customers = Customer::find()->active()->all(); $this->assertEquals(2, count($customers)); + $customers = Customer::find(array( + 'scopes' => array('active'), + ))->all(); + $this->assertEquals(2, count($customers)); + + // asArray + $customers = Customer::find()->orderBy('id')->asArray()->all(); + $this->assertEquals('user2', $customers[1]['name']); + + // indexBy + $customers = Customer::find()->orderBy('id')->indexBy('name')->all(); } public function testEagerLoading()