mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 13:58:55 +08:00
...
This commit is contained in:
@ -68,7 +68,6 @@ class ActiveFinder extends \yii\base\Object
|
|||||||
} else {
|
} else {
|
||||||
$this->initFrom($query);
|
$this->initFrom($query);
|
||||||
$this->applyScopes($query);
|
$this->applyScopes($query);
|
||||||
// todo: filter
|
|
||||||
$sql = $this->connection->getQueryBuilder()->build($query);
|
$sql = $this->connection->getQueryBuilder()->build($query);
|
||||||
if (strpos($sql, '@.') !== false) {
|
if (strpos($sql, '@.') !== false) {
|
||||||
if ($query->tableAlias !== null) {
|
if ($query->tableAlias !== null) {
|
||||||
@ -189,10 +188,7 @@ class ActiveFinder extends \yii\base\Object
|
|||||||
{
|
{
|
||||||
if (is_array($query->scopes)) {
|
if (is_array($query->scopes)) {
|
||||||
$class = $query->modelClass;
|
$class = $query->modelClass;
|
||||||
$defaultScope = $class::defaultScope();
|
$class::defaultScope($query);
|
||||||
if ($defaultScope !== null) {
|
|
||||||
call_user_func_array($defaultScope, array($query));
|
|
||||||
}
|
|
||||||
$scopes = $class::scopes();
|
$scopes = $class::scopes();
|
||||||
foreach ($query->scopes as $name => $params) {
|
foreach ($query->scopes as $name => $params) {
|
||||||
if (is_integer($name)) {
|
if (is_integer($name)) {
|
||||||
|
|||||||
@ -11,7 +11,6 @@
|
|||||||
namespace yii\db\ar;
|
namespace yii\db\ar;
|
||||||
|
|
||||||
use yii\base\VectorIterator;
|
use yii\base\VectorIterator;
|
||||||
use yii\db\dao\BaseQuery;
|
|
||||||
use yii\db\dao\Expression;
|
use yii\db\dao\Expression;
|
||||||
use yii\db\Exception;
|
use yii\db\Exception;
|
||||||
|
|
||||||
@ -39,38 +38,8 @@ use yii\db\Exception;
|
|||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @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.
|
* @var string the SQL statement to be executed to retrieve primary records.
|
||||||
* This is set by [[ActiveRecord::findBySql()]].
|
* 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.
|
* 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.
|
* @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.
|
* 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
|
* 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).
|
* 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}.
|
* of {@link CDbCriteria}.
|
||||||
*/
|
*/
|
||||||
public static function defaultScope()
|
public static function defaultScope($query)
|
||||||
{
|
{
|
||||||
return array();
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -10,24 +10,18 @@
|
|||||||
|
|
||||||
namespace yii\db\ar;
|
namespace yii\db\ar;
|
||||||
|
|
||||||
use yii\db\dao\BaseQuery;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ActiveRelation represents the specification of a relation declared in [[ActiveRecord::relations()]].
|
* ActiveRelation represents the specification of a relation declared in [[ActiveRecord::relations()]].
|
||||||
*
|
*
|
||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
class ActiveRelation extends BaseQuery
|
class ActiveRelation extends BaseActiveQuery
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string the name of this relation
|
* @var string the name of this relation
|
||||||
*/
|
*/
|
||||||
public $name;
|
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
|
* @var boolean whether this relation is a one-many relation
|
||||||
*/
|
*/
|
||||||
@ -37,15 +31,6 @@ class ActiveRelation extends BaseQuery
|
|||||||
* this relation is used to load related records, and 'INNER JOIN' when this relation is used as a filter.
|
* this relation is used to load related records, and 'INNER JOIN' when this relation is used as a filter.
|
||||||
*/
|
*/
|
||||||
public $joinType;
|
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
|
* @var string the ON clause of the join query
|
||||||
*/
|
*/
|
||||||
@ -54,16 +39,4 @@ class ActiveRelation extends BaseQuery
|
|||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $via;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
81
framework/db/ar/BaseActiveQuery.php
Normal file
81
framework/db/ar/BaseActiveQuery.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ActiveFinder class file.
|
||||||
|
*
|
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
|
* @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 <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
|
// scopes
|
||||||
$customers = Customer::find()->active()->all();
|
$customers = Customer::find()->active()->all();
|
||||||
$this->assertEquals(2, count($customers));
|
$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()
|
public function testEagerLoading()
|
||||||
|
|||||||
Reference in New Issue
Block a user