Commit 50743935 by Qiang Xue

...

parent a53acdf6
......@@ -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)) {
......
......@@ -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 <qiang.xue@gmail.com>
* @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.
......
......@@ -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;
}
/**
......
......@@ -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 <qiang.xue@gmail.com>
* @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;
}
<?php
/**
* ActiveFinder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
use yii\db\dao\BaseQuery;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @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;
}
}
......@@ -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()
......
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