You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
6.6 KiB
274 lines
6.6 KiB
13 years ago
|
<?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/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\db;
|
||
13 years ago
|
|
||
12 years ago
|
use yii\db\Connection;
|
||
|
use yii\db\Command;
|
||
|
use yii\db\QueryBuilder;
|
||
|
use yii\db\BaseQuery;
|
||
13 years ago
|
use yii\base\VectorIterator;
|
||
12 years ago
|
use yii\db\Expression;
|
||
13 years ago
|
use yii\db\Exception;
|
||
|
|
||
12 years ago
|
class ActiveQuery extends BaseQuery
|
||
12 years ago
|
{
|
||
|
/**
|
||
|
* @var string the name of the ActiveRecord class.
|
||
|
*/
|
||
|
public $modelClass;
|
||
|
/**
|
||
|
* @var array list of relations that this query should be performed with
|
||
|
*/
|
||
12 years ago
|
public $with;
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var string the name of the column by which the query result should be indexed.
|
||
|
* This is only used when the query result is returned as an array when calling [[all()]].
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $indexBy;
|
||
12 years ago
|
/**
|
||
|
* @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
|
||
|
*/
|
||
12 years ago
|
public $scopes;
|
||
12 years ago
|
/**
|
||
|
* @var string the SQL statement to be executed for retrieving AR records.
|
||
|
* This is set by [[ActiveRecord::findBySql()]].
|
||
|
*/
|
||
|
public $sql;
|
||
|
|
||
|
public function __call($name, $params)
|
||
|
{
|
||
|
if (method_exists($this->modelClass, $name)) {
|
||
|
$this->scopes[$name] = $params;
|
||
|
return $this;
|
||
|
} else {
|
||
|
return parent::__call($name, $params);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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.
|
||
|
*/
|
||
|
public function all()
|
||
|
{
|
||
12 years ago
|
$command = $this->createCommand();
|
||
|
$rows = $command->queryAll();
|
||
12 years ago
|
if ($rows !== array()) {
|
||
|
$models = $this->createModels($rows);
|
||
|
if (!empty($this->with)) {
|
||
|
$this->fetchRelatedModels($models, $this->with);
|
||
|
}
|
||
|
return $models;
|
||
|
} else {
|
||
12 years ago
|
return array();
|
||
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Executes query and returns a single row of result.
|
||
12 years ago
|
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
|
||
|
* the query result may be either an array or an ActiveRecord object. Null will be returned
|
||
12 years ago
|
* if the query results in nothing.
|
||
|
*/
|
||
|
public function one()
|
||
|
{
|
||
12 years ago
|
$command = $this->createCommand();
|
||
|
$row = $command->queryRow();
|
||
12 years ago
|
if ($row !== false && !$this->asArray) {
|
||
12 years ago
|
/** @var $class ActiveRecord */
|
||
12 years ago
|
$class = $this->modelClass;
|
||
12 years ago
|
$model = $class::create($row);
|
||
|
if (!empty($this->with)) {
|
||
12 years ago
|
$models = array($model);
|
||
|
$this->fetchRelatedModels($models, $this->with);
|
||
|
$model = $models[0];
|
||
12 years ago
|
}
|
||
|
return $model;
|
||
12 years ago
|
} else {
|
||
|
return $row === false ? null : $row;
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a scalar value for this query.
|
||
|
* The value returned will be the first column in the first row of the query results.
|
||
|
* @return string|boolean the value of the first column in the first row of the query result.
|
||
|
* False is returned if there is no value.
|
||
|
*/
|
||
|
public function value()
|
||
|
{
|
||
12 years ago
|
return $this->createCommand()->queryScalar();
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Executes query and returns if matching row exists in the table.
|
||
|
* @return bool if row exists in the table.
|
||
|
*/
|
||
|
public function exists()
|
||
|
{
|
||
12 years ago
|
$this->select = array(new Expression('1'));
|
||
|
return $this->value() !== false;
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Creates a DB command that can be used to execute this query.
|
||
|
* @return Command the created DB command instance.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function createCommand()
|
||
12 years ago
|
{
|
||
12 years ago
|
/** @var $modelClass ActiveRecord */
|
||
12 years ago
|
$modelClass = $this->modelClass;
|
||
12 years ago
|
$db = $modelClass::getDbConnection();
|
||
12 years ago
|
if ($this->sql === null) {
|
||
|
if ($this->from === null) {
|
||
|
$tableName = $modelClass::tableName();
|
||
|
$this->from = array($tableName);
|
||
|
}
|
||
|
if (!empty($this->scopes)) {
|
||
|
foreach ($this->scopes as $name => $config) {
|
||
|
if (is_integer($name)) {
|
||
|
$modelClass::$config($this);
|
||
|
} else {
|
||
|
array_unshift($config, $this);
|
||
|
call_user_func_array(array($modelClass, $name), $config);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/** @var $qb QueryBuilder */
|
||
|
$qb = $db->getQueryBuilder();
|
||
12 years ago
|
$this->sql = $qb->build($this);
|
||
12 years ago
|
}
|
||
12 years ago
|
return $db->createCommand($this->sql, $this->params);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
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;
|
||
|
}
|
||
|
|
||
12 years ago
|
public function indexBy($column)
|
||
12 years ago
|
{
|
||
12 years ago
|
$this->indexBy = $column;
|
||
12 years ago
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function scopes($names)
|
||
|
{
|
||
|
$this->scopes = $names;
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
12 years ago
|
protected function createModels($rows)
|
||
|
{
|
||
|
$models = array();
|
||
|
if ($this->asArray) {
|
||
12 years ago
|
if ($this->indexBy === null) {
|
||
12 years ago
|
return $rows;
|
||
|
}
|
||
|
foreach ($rows as $row) {
|
||
12 years ago
|
$models[$row[$this->indexBy]] = $row;
|
||
12 years ago
|
}
|
||
|
} else {
|
||
|
/** @var $class ActiveRecord */
|
||
|
$class = $this->modelClass;
|
||
12 years ago
|
if ($this->indexBy === null) {
|
||
12 years ago
|
foreach ($rows as $row) {
|
||
|
$models[] = $class::create($row);
|
||
|
}
|
||
|
} else {
|
||
|
foreach ($rows as $row) {
|
||
|
$model = $class::create($row);
|
||
12 years ago
|
$models[$model->{$this->indexBy}] = $model;
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
return $models;
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
protected function fetchRelatedModels(&$models, $with)
|
||
12 years ago
|
{
|
||
|
$primaryModel = new $this->modelClass;
|
||
12 years ago
|
$relations = $this->normalizeRelations($primaryModel, $with);
|
||
|
foreach ($relations as $name => $relation) {
|
||
12 years ago
|
if ($relation->asArray === null) {
|
||
|
// inherit asArray from primary query
|
||
|
$relation->asArray = $this->asArray;
|
||
|
}
|
||
12 years ago
|
$relation->findWith($name, $models);
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
|
* @param ActiveRecord $model
|
||
|
* @param array $with
|
||
|
* @return ActiveRelation[]
|
||
|
* @throws \yii\db\Exception
|
||
|
*/
|
||
|
protected function normalizeRelations($model, $with)
|
||
|
{
|
||
|
$relations = array();
|
||
|
foreach ($with as $name => $options) {
|
||
|
if (is_integer($name)) {
|
||
|
$name = $options;
|
||
|
$options = array();
|
||
|
}
|
||
|
if (($pos = strpos($name, '.')) !== false) {
|
||
|
// with sub-relations
|
||
|
$childName = substr($name, $pos + 1);
|
||
|
$name = substr($name, 0, $pos);
|
||
|
} else {
|
||
|
$childName = null;
|
||
|
}
|
||
|
|
||
|
if (!isset($relations[$name])) {
|
||
|
if (!method_exists($model, $name)) {
|
||
|
throw new Exception("Unknown relation: $name");
|
||
|
}
|
||
|
/** @var $relation ActiveRelation */
|
||
|
$relation = $model->$name();
|
||
|
$relation->primaryModel = null;
|
||
|
$relations[$name] = $relation;
|
||
|
} else {
|
||
|
$relation = $relations[$name];
|
||
|
}
|
||
|
|
||
|
if (isset($childName)) {
|
||
|
if (isset($relation->with[$childName])) {
|
||
|
$relation->with[$childName] = array_merge($relation->with, $options);
|
||
|
} else {
|
||
|
$relation->with[$childName] = $options;
|
||
|
}
|
||
|
} else {
|
||
|
foreach ($options as $p => $v) {
|
||
|
$relation->$p = $v;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $relations;
|
||
|
}
|
||
12 years ago
|
}
|