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.

272 lines
6.7 KiB

13 years ago
<?php
/**
* ActiveFinder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
13 years ago
use yii\db\Connection;
use yii\db\Command;
use yii\db\QueryBuilder;
13 years ago
use yii\base\VectorIterator;
use yii\db\Expression;
13 years ago
use yii\db\Exception;
12 years ago
class ActiveQuery extends Query
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();
if ($rows !== array()) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
12 years ago
$this->populateRelations($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();
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);
12 years ago
$this->populateRelations($models, $this->with);
12 years ago
$model = $models[0];
12 years ago
}
return $model;
} 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.
12 years ago
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
12 years ago
* @return Command the created DB command instance.
12 years ago
*/
12 years ago
public function createCommand($db = null)
12 years ago
{
12 years ago
/** @var $modelClass ActiveRecord */
12 years ago
$modelClass = $this->modelClass;
12 years ago
if ($db === null) {
$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)
{
12 years ago
$this->scopes = func_get_args();
if (isset($this->scopes[0]) && is_array($this->scopes[0])) {
// the parameter is given as an array
$this->scopes = $this->scopes[0];
}
12 years ago
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 populateRelations(&$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();
12 years ago
foreach ($with as $name => $callback) {
12 years ago
if (is_integer($name)) {
12 years ago
$name = $callback;
$callback = null;
12 years ago
}
if (($pos = strpos($name, '.')) !== false) {
// with sub-relations
$childName = substr($name, $pos + 1);
$name = substr($name, 0, $pos);
} else {
$childName = null;
}
$t = strtolower($name);
if (!isset($relations[$t])) {
$relation = $model->getRelation($name);
$relation->primaryModel = null;
$relations[$t] = $relation;
12 years ago
} else {
$relation = $relations[$t];
12 years ago
}
if (isset($childName)) {
12 years ago
$relation->with[$childName] = $callback;
} elseif ($callback !== null) {
call_user_func($callback, $relation);
12 years ago
}
}
return $relations;
}
12 years ago
}