Yii2 Bootstrap 3
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.

383 lines
11 KiB

13 years ago
<?php
/**
12 years ago
* ActiveQuery class file.
13 years ago
*
* @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;
use yii\db\Expression;
13 years ago
use yii\db\Exception;
12 years ago
/**
* ActiveQuery represents a DB query associated with an Active Record class.
*
* ActiveQuery instances are usually created by [[ActiveRecord::find()]], [[ActiveRecord::findBySql()]]
* and [[ActiveRecord::count()]].
*
* ActiveQuery mainly provides the following methods to retrieve the query results:
*
* - [[one()]]: returns a single record populated with the first row of data.
* - [[all()]]: returns all records based on the query results.
* - [[count()]]: returns the number of records.
* - [[sum()]]: returns the sum over the specified column.
* - [[average()]]: returns the average over the specified column.
* - [[min()]]: returns the min over the specified column.
* - [[max()]]: returns the max over the specified column.
* - [[scalar()]]: returns the value of the first column in the first row of the query result.
12 years ago
* - [[exists()]]: returns a value indicating whether the query result has data or not.
*
* Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
* [[orderBy()]] to customize the query options.
*
* ActiveQuery also provides the following additional query options:
*
12 years ago
* - [[with()]]: list of relations that this query should be performed with.
* - [[indexBy()]]: the name of the column by which the query result should be indexed.
* - [[asArray()]]: whether to return each record as an array.
12 years ago
*
* These options can be configured using methods of the same name. For example:
*
* ~~~
* $customers = Customer::find()->with('orders')->asArray()->all();
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
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 query results should be indexed by.
12 years ago
* 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 string the SQL statement to be executed for retrieving AR records.
* This is set by [[ActiveRecord::findBySql()]].
*/
public $sql;
/**
* PHP magic method.
* This method allows calling static method defined in [[modelClass]] via this query object.
* It is mainly implemented for supporting the feature of scope.
* @param string $name the method name to be called
* @param array $params the parameters passed to the method
* @return mixed the method return result
*/
public function __call($name, $params)
{
if (method_exists($this->modelClass, $name)) {
array_unshift($params, $this);
return call_user_func_array(array($this->modelClass, $name), $params);
} else {
return parent::__call($name, $params);
}
}
/**
12 years ago
* 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 the number of records.
* @param string $q the COUNT expression. Defaults to '*'.
* Make sure you properly quote column names.
* @return integer number of records
*/
public function count($q = '*')
{
$this->select = array("COUNT($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the sum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the sum of the specified column values
*/
public function sum($q)
{
$this->select = array("SUM($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the average of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the average of the specified column values.
*/
public function average($q)
{
$this->select = array("AVG($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the minimum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the minimum of the specified column values.
*/
public function min($q)
{
$this->select = array("MIN($q)");
return $this->createCommand()->queryScalar();
}
/**
* Returns the maximum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly quote column names.
* @return integer the maximum of the specified column values.
*/
public function max($q)
{
$this->select = array("MAX($q)");
return $this->createCommand()->queryScalar();
}
/**
12 years ago
* Returns the query result as a scalar value.
12 years ago
* 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.
12 years ago
* False is returned if the query result is empty.
12 years ago
*/
public function scalar()
12 years ago
{
12 years ago
return $this->createCommand()->queryScalar();
12 years ago
}
/**
12 years ago
* Returns a value indicating whether the query result contains any row of data.
* @return boolean whether the query result contains any row of data.
12 years ago
*/
public function exists()
{
12 years ago
$this->select = array(new Expression('1'));
return $this->scalar() !== 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);
}
/** @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
/**
* Sets the [[asArray]] property.
* @param boolean $value whether to return the query results in terms of arrays instead of Active Records.
* @return ActiveQuery the query object itself
*/
12 years ago
public function asArray($value = true)
{
$this->asArray = $value;
return $this;
}
12 years ago
/**
* Specifies the relations with which this query should be performed.
*
* The parameters to this method can be either one or multiple strings, or a single array
* of relation names and the optional callbacks to customize the relations.
*
* The followings are some usage examples:
*
* ~~~
* // find customers together with their orders and country
* Customer::find()->with('orders', 'country')->all();
* // find customers together with their country and orders of status 1
* Customer::find()->with(array(
* 'orders' => function($query) {
* $query->andWhere('status = 1');
* },
* 'country',
* ))->all();
* ~~~
*
* @return ActiveQuery the query object itself
*/
12 years ago
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
/**
* Sets the [[indexBy]] property.
* @param string $column the name of the column by which the query results should be indexed by.
* @return ActiveQuery the query object itself
*/
12 years ago
public function indexBy($column)
12 years ago
{
12 years ago
$this->indexBy = $column;
12 years ago
return $this;
}
12 years ago
private function createModels($rows)
12 years ago
{
$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
private 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[]
*/
12 years ago
private function normalizeRelations($model, $with)
12 years ago
{
$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
}