|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
-
-
- namespace yii\db;
-
-
- trait ActiveQueryTrait
- {
-
-
- public $modelClass;
-
-
- public $with;
-
-
- public $asArray;
-
-
-
-
- public function asArray($value = true)
- {
- $this->asArray = $value;
- return $this;
- }
-
-
-
- public function with()
- {
- $with = func_get_args();
- if (isset($with[0]) && is_array($with[0])) {
-
- $with = $with[0];
- }
-
- if (empty($this->with)) {
- $this->with = $with;
- } elseif (!empty($with)) {
- foreach ($with as $name => $value) {
- if (is_int($name)) {
-
- $this->with[] = $value;
- } else {
- $this->with[$name] = $value;
- }
- }
- }
-
- return $this;
- }
-
-
-
- private function createModels($rows)
- {
- $models = [];
- if ($this->asArray) {
- if ($this->indexBy === null) {
- return $rows;
- }
- foreach ($rows as $row) {
- if (is_string($this->indexBy)) {
- $key = $row[$this->indexBy];
- } else {
- $key = call_user_func($this->indexBy, $row);
- }
- $models[$key] = $row;
- }
- } else {
-
- $class = $this->modelClass;
- if ($this->indexBy === null) {
- foreach ($rows as $row) {
- $model = $class::instantiate($row);
- $modelClass = get_class($model);
- $modelClass::populateRecord($model, $row);
- $models[] = $model;
- }
- } else {
- foreach ($rows as $row) {
- $model = $class::instantiate($row);
- $modelClass = get_class($model);
- $modelClass::populateRecord($model, $row);
- if (is_string($this->indexBy)) {
- $key = $model->{$this->indexBy};
- } else {
- $key = call_user_func($this->indexBy, $model);
- }
- $models[$key] = $model;
- }
- }
- }
-
- return $models;
- }
-
-
-
- public function findWith($with, &$models)
- {
- $primaryModel = reset($models);
- if (!$primaryModel instanceof ActiveRecordInterface) {
- $primaryModel = new $this->modelClass;
- }
- $relations = $this->normalizeRelations($primaryModel, $with);
-
- foreach ($relations as $name => $relation) {
- if ($relation->asArray === null) {
-
- $relation->asArray($this->asArray);
- }
- $relation->populateRelation($name, $models);
- }
- }
-
-
-
- private function normalizeRelations($model, $with)
- {
- $relations = [];
- foreach ($with as $name => $callback) {
- if (is_int($name)) {
- $name = $callback;
- $callback = null;
- }
- if (($pos = strpos($name, '.')) !== false) {
-
- $childName = substr($name, $pos + 1);
- $name = substr($name, 0, $pos);
- } else {
- $childName = null;
- }
-
- if (!isset($relations[$name])) {
- $relation = $model->getRelation($name);
- $relation->primaryModel = null;
- $relations[$name] = $relation;
- } else {
- $relation = $relations[$name];
- }
-
- if (isset($childName)) {
- $relation->with[$childName] = $callback;
- } elseif ($callback !== null) {
- call_user_func($callback, $relation);
- }
- }
-
- return $relations;
- }
- }
|