|
- <?php
-
-
- namespace yii\db;
-
- use yii\base\InvalidConfigException;
-
-
- class ActiveQuery extends Query implements ActiveQueryInterface
- {
- use ActiveQueryTrait;
- use ActiveRelationTrait;
-
-
-
- const EVENT_INIT = 'init';
-
-
-
- public $sql;
-
-
- public $on;
-
-
- public $joinWith;
-
-
-
-
- public function __construct($modelClass, $config = [])
- {
- $this->modelClass = $modelClass;
- parent::__construct($config);
- }
-
-
-
- public function init()
- {
- parent::init();
- $this->trigger(self::EVENT_INIT);
- }
-
-
-
- public function all($db = null)
- {
- return parent::all($db);
- }
-
-
-
- public function prepare($builder)
- {
-
-
-
-
- if (!empty($this->joinWith)) {
- $this->buildJoinWith();
- $this->joinWith = null;
- }
-
- if (empty($this->from)) {
-
- $modelClass = $this->modelClass;
- $tableName = $modelClass::tableName();
- $this->from = [$tableName];
- }
-
- if (empty($this->select) && !empty($this->join)) {
- list(, $alias) = $this->getQueryTableName($this);
- $this->select = ["$alias.*"];
- }
-
- if ($this->primaryModel === null) {
-
- $query = Query::create($this);
- } else {
-
- $where = $this->where;
-
- if ($this->via instanceof self) {
-
- $viaModels = $this->via->findJunctionRows([$this->primaryModel]);
- $this->filterByModels($viaModels);
- } elseif (is_array($this->via)) {
-
-
- list($viaName, $viaQuery) = $this->via;
- if ($viaQuery->multiple) {
- $viaModels = $viaQuery->all();
- $this->primaryModel->populateRelation($viaName, $viaModels);
- } else {
- $model = $viaQuery->one();
- $this->primaryModel->populateRelation($viaName, $model);
- $viaModels = $model === null ? [] : [$model];
- }
- $this->filterByModels($viaModels);
- } else {
- $this->filterByModels([$this->primaryModel]);
- }
-
- $query = Query::create($this);
- $this->where = $where;
- }
-
- if (!empty($this->on)) {
- $query->andWhere($this->on);
- }
-
- return $query;
- }
-
-
-
- public function populate($rows)
- {
- if (empty($rows)) {
- return [];
- }
-
- $models = $this->createModels($rows);
- if (!empty($this->join) && $this->indexBy === null) {
- $models = $this->removeDuplicatedModels($models);
- }
- if (!empty($this->with)) {
- $this->findWith($this->with, $models);
- }
-
- if ($this->inverseOf !== null) {
- $this->addInverseRelations($models);
- }
-
- if (!$this->asArray) {
- foreach ($models as $model) {
- $model->afterFind();
- }
- }
-
- return $models;
- }
-
-
-
- private function removeDuplicatedModels($models)
- {
- $hash = [];
-
- $class = $this->modelClass;
- $pks = $class::primaryKey();
-
- if (count($pks) > 1) {
-
- foreach ($models as $i => $model) {
- $key = [];
- foreach ($pks as $pk) {
- if (!isset($model[$pk])) {
-
- break 2;
- }
- $key[] = $model[$pk];
- }
- $key = serialize($key);
- if (isset($hash[$key])) {
- unset($models[$i]);
- } else {
- $hash[$key] = true;
- }
- }
- } elseif (empty($pks)) {
- throw new InvalidConfigException("Primary key of '{$class}' can not be empty.");
- } else {
-
- $pk = reset($pks);
- foreach ($models as $i => $model) {
- if (!isset($model[$pk])) {
-
- break;
- }
- $key = $model[$pk];
- if (isset($hash[$key])) {
- unset($models[$i]);
- } elseif ($key !== null) {
- $hash[$key] = true;
- }
- }
- }
-
- return array_values($models);
- }
-
-
-
- public function one($db = null)
- {
- $row = parent::one($db);
- if ($row !== false) {
- $models = $this->populate([$row]);
- return reset($models) ?: null;
- } else {
- return null;
- }
- }
-
-
-
- public function createCommand($db = null)
- {
-
- $modelClass = $this->modelClass;
- if ($db === null) {
- $db = $modelClass::getDb();
- }
-
- if ($this->sql === null) {
- list ($sql, $params) = $db->getQueryBuilder()->build($this);
- } else {
- $sql = $this->sql;
- $params = $this->params;
- }
-
- return $db->createCommand($sql, $params);
- }
-
-
-
- protected function queryScalar($selectExpression, $db)
- {
- if ($this->sql === null) {
- return parent::queryScalar($selectExpression, $db);
- }
-
- $modelClass = $this->modelClass;
- if ($db === null) {
- $db = $modelClass::getDb();
- }
- return (new Query)->select([$selectExpression])
- ->from(['c' => "({$this->sql})"])
- ->params($this->params)
- ->createCommand($db)
- ->queryScalar();
- }
-
-
-
- public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN')
- {
- $relations = [];
- foreach ((array) $with as $name => $callback) {
- if (is_int($name)) {
- $name = $callback;
- $callback = null;
- }
-
- if (preg_match('/^(.*?)(?:\s+AS\s+|\s+)(\w+)$/i', $name, $matches)) {
-
- list(, $relation, $alias) = $matches;
- $name = $relation;
- $callback = function ($query) use ($callback, $alias) {
-
- $query->alias($alias);
- if ($callback !== null) {
- call_user_func($callback, $query);
- }
- };
- }
-
- if ($callback === null) {
- $relations[] = $name;
- } else {
- $relations[$name] = $callback;
- }
- }
- $this->joinWith[] = [$relations, $eagerLoading, $joinType];
- return $this;
- }
-
- private function buildJoinWith()
- {
- $join = $this->join;
- $this->join = [];
-
- $model = new $this->modelClass;
- foreach ($this->joinWith as $config) {
- list ($with, $eagerLoading, $joinType) = $config;
- $this->joinWithRelations($model, $with, $joinType);
-
- if (is_array($eagerLoading)) {
- foreach ($with as $name => $callback) {
- if (is_int($name)) {
- if (!in_array($callback, $eagerLoading, true)) {
- unset($with[$name]);
- }
- } elseif (!in_array($name, $eagerLoading, true)) {
- unset($with[$name]);
- }
- }
- } elseif (!$eagerLoading) {
- $with = [];
- }
-
- $this->with($with);
- }
-
-
-
- $uniqueJoins = [];
- foreach ($this->join as $j) {
- $uniqueJoins[serialize($j)] = $j;
- }
- $this->join = array_values($uniqueJoins);
-
- if (!empty($join)) {
-
-
- $this->join = empty($this->join) ? $join : array_merge($this->join, $join);
- }
- }
-
-
-
- public function innerJoinWith($with, $eagerLoading = true)
- {
- return $this->joinWith($with, $eagerLoading, 'INNER JOIN');
- }
-
-
-
- private function joinWithRelations($model, $with, $joinType)
- {
- $relations = [];
-
- foreach ($with as $name => $callback) {
- if (is_int($name)) {
- $name = $callback;
- $callback = null;
- }
-
- $primaryModel = $model;
- $parent = $this;
- $prefix = '';
- while (($pos = strpos($name, '.')) !== false) {
- $childName = substr($name, $pos + 1);
- $name = substr($name, 0, $pos);
- $fullName = $prefix === '' ? $name : "$prefix.$name";
- if (!isset($relations[$fullName])) {
- $relations[$fullName] = $relation = $primaryModel->getRelation($name);
- $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
- } else {
- $relation = $relations[$fullName];
- }
- $primaryModel = new $relation->modelClass;
- $parent = $relation;
- $prefix = $fullName;
- $name = $childName;
- }
-
- $fullName = $prefix === '' ? $name : "$prefix.$name";
- if (!isset($relations[$fullName])) {
- $relations[$fullName] = $relation = $primaryModel->getRelation($name);
- if ($callback !== null) {
- call_user_func($callback, $relation);
- }
- if (!empty($relation->joinWith)) {
- $relation->buildJoinWith();
- }
- $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
- }
- }
- }
-
-
-
- private function getJoinType($joinType, $name)
- {
- if (is_array($joinType) && isset($joinType[$name])) {
- return $joinType[$name];
- } else {
- return is_string($joinType) ? $joinType : 'INNER JOIN';
- }
- }
-
-
-
- private function getQueryTableName($query)
- {
- if (empty($query->from)) {
-
- $modelClass = $query->modelClass;
- $tableName = $modelClass::tableName();
- } else {
- $tableName = '';
- foreach ($query->from as $alias => $tableName) {
- if (is_string($alias)) {
- return [$tableName, $alias];
- } else {
- break;
- }
- }
- }
-
- if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) {
- $alias = $matches[2];
- } else {
- $alias = $tableName;
- }
-
- return [$tableName, $alias];
- }
-
-
-
- private function joinWithRelation($parent, $child, $joinType)
- {
- $via = $child->via;
- $child->via = null;
- if ($via instanceof ActiveQuery) {
-
- $this->joinWithRelation($parent, $via, $joinType);
- $this->joinWithRelation($via, $child, $joinType);
- return;
- } elseif (is_array($via)) {
-
- $this->joinWithRelation($parent, $via[1], $joinType);
- $this->joinWithRelation($via[1], $child, $joinType);
- return;
- }
-
- list ($parentTable, $parentAlias) = $this->getQueryTableName($parent);
- list ($childTable, $childAlias) = $this->getQueryTableName($child);
-
- if (!empty($child->link)) {
-
- if (strpos($parentAlias, '{{') === false) {
- $parentAlias = '{{' . $parentAlias . '}}';
- }
- if (strpos($childAlias, '{{') === false) {
- $childAlias = '{{' . $childAlias . '}}';
- }
-
- $on = [];
- foreach ($child->link as $childColumn => $parentColumn) {
- $on[] = "$parentAlias.[[$parentColumn]] = $childAlias.[[$childColumn]]";
- }
- $on = implode(' AND ', $on);
- if (!empty($child->on)) {
- $on = ['and', $on, $child->on];
- }
- } else {
- $on = $child->on;
- }
- $this->join($joinType, empty($child->from) ? $childTable : $child->from, $on);
-
- if (!empty($child->where)) {
- $this->andWhere($child->where);
- }
- if (!empty($child->having)) {
- $this->andHaving($child->having);
- }
- if (!empty($child->orderBy)) {
- $this->addOrderBy($child->orderBy);
- }
- if (!empty($child->groupBy)) {
- $this->addGroupBy($child->groupBy);
- }
- if (!empty($child->params)) {
- $this->addParams($child->params);
- }
- if (!empty($child->join)) {
- foreach ($child->join as $join) {
- $this->join[] = $join;
- }
- }
- if (!empty($child->union)) {
- foreach ($child->union as $union) {
- $this->union[] = $union;
- }
- }
- }
-
-
-
- public function onCondition($condition, $params = [])
- {
- $this->on = $condition;
- $this->addParams($params);
- return $this;
- }
-
-
-
- public function andOnCondition($condition, $params = [])
- {
- if ($this->on === null) {
- $this->on = $condition;
- } else {
- $this->on = ['and', $this->on, $condition];
- }
- $this->addParams($params);
- return $this;
- }
-
-
-
- public function orOnCondition($condition, $params = [])
- {
- if ($this->on === null) {
- $this->on = $condition;
- } else {
- $this->on = ['or', $this->on, $condition];
- }
- $this->addParams($params);
- return $this;
- }
-
-
-
- public function viaTable($tableName, $link, callable $callable = null)
- {
- $relation = new ActiveQuery(get_class($this->primaryModel), [
- 'from' => [$tableName],
- 'link' => $link,
- 'multiple' => true,
- 'asArray' => true,
- ]);
- $this->via = $relation;
- if ($callable !== null) {
- call_user_func($callable, $relation);
- }
-
- return $this;
- }
-
-
-
- public function alias($alias)
- {
- if (empty($this->from) || count($this->from) < 2) {
- list($tableName, ) = $this->getQueryTableName($this);
- $this->from = [$alias => $tableName];
- } else {
-
- $modelClass = $this->modelClass;
- $tableName = $modelClass::tableName();
-
- foreach ($this->from as $key => $table) {
- if ($table === $tableName) {
- unset($this->from[$key]);
- $this->from[$alias] = $tableName;
- }
- }
- }
- return $this;
- }
- }
|