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.

208 lines
5.5 KiB

13 years ago
<?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;
12 years ago
use yii\db\dao\BaseQuery;
13 years ago
use yii\base\VectorIterator;
13 years ago
use yii\db\dao\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 = array();
12 years ago
/**
* @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 $index;
/**
* @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 = array();
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
return $this->find();
12 years ago
}
/**
* Executes query and returns a single row of result.
* @return null|array|ActiveRecord the 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
* if the query results in nothing.
*/
public function one()
{
12 years ago
$records = $this->find();
12 years ago
return isset($records[0]) ? $records[0] : null;
}
/**
* 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()
{
return $this->createFinder()->find($this, true);
}
/**
* Executes query and returns if matching row exists in the table.
* @return bool if row exists in the table.
*/
public function exists()
{
return $this->select(array(new Expression('1')))->value() !== false;
}
/**
* Returns the database connection used by this query.
* This method returns the connection used by the [[modelClass]].
* @return \yii\db\dao\Connection the database connection used by this query
*/
public function getDbConnection()
{
$class = $this->modelClass;
return $class::getDbConnection();
}
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;
}
public function index($column)
{
$this->index = $column;
return $this;
}
public function scopes($names)
{
$this->scopes = $names;
return $this;
}
12 years ago
/**
* Sets the parameters about query caching.
* This is a shortcut method to {@link CDbConnection::cache()}.
* It changes the query caching parameter of the {@link dbConnection} instance.
* @param integer $duration the number of seconds that query results may remain valid in cache.
* If this is 0, the caching will be disabled.
* @param \yii\caching\Dependency $dependency the dependency that will be used when saving the query results into cache.
* @param integer $queryCount number of SQL queries that need to be cached after calling this method. Defaults to 1,
* meaning that the next SQL query will be cached.
* @return ActiveRecord the active record instance itself.
*/
public function cache($duration, $dependency = null, $queryCount = 1)
{
$this->getDbConnection()->cache($duration, $dependency, $queryCount);
return $this;
}
12 years ago
protected function find()
12 years ago
{
12 years ago
$modelClass = $this->modelClass;
12 years ago
/**
12 years ago
* @var ActiveRecord $model
12 years ago
*/
12 years ago
$model = $modelClass::model();
/**
* @var \yii\db\dao\Connection $db
*/
$db = $model->getDbConnection();
12 years ago
if ($this->sql === null) {
if ($this->from === null) {
12 years ago
$tableName = $model->getTableSchema()->name;
12 years ago
$this->from = array($tableName);
}
12 years ago
foreach ($this->scopes as $name => $config) {
if (is_integer($name)) {
$model->$config($this);
} else {
array_unshift($config, $this);
call_user_func_array(array($model, $name), $config);
}
}
$this->sql = $db->getQueryBuilder()->build($this);
12 years ago
}
12 years ago
$command = $db->createCommand($this->sql, $this->params);
12 years ago
$rows = $command->queryAll();
12 years ago
$records = $this->createRecords($rows);
12 years ago
if ($records !== array()) {
foreach ($this->with as $name => $config) {
12 years ago
/** @var Relation $relation */
12 years ago
$relation = $model->$name();
foreach ($config as $p => $v) {
$relation->$p = $v;
}
12 years ago
if ($relation->asArray === null) {
// inherit asArray from parent query
$relation->asArray = $this->asArray;
12 years ago
}
12 years ago
$rs = $relation->findWith($records);
/*
foreach ($rs as $r) {
// find the matching parent record(s)
// insert into the parent records(s)
12 years ago
}
12 years ago
*/
12 years ago
}
}
12 years ago
12 years ago
return $records;
}
}