Populate relation
@param string $name
@param array||\yii\db\ActiveRecord||\yii\db\ActiveRecord[] $values
@return boolean
967865
public function setRelated($name, $values)
    {
        $relation = $this->owner->getRelation($name, false);
        if ($relation === null) {
            return false;
        }
        $class = $relation->modelClass;
        $multiple = $relation->multiple;
        $link = $relation->link;
        $uniqueKeys = array_flip($class::primaryKey());
        foreach (array_keys($link) as $from) {
            unset($uniqueKeys[$from]);
        }
        $uniqueKeys = array_keys($uniqueKeys);
        if (isset($this->_original_relations[$name])) {
            $children = $this->_original_relations[$name];
        } else {
            $this->_original_relations[$name] = $children = $this->owner->$name;
        }

        if ($multiple) {
            $newChildren = [];
            foreach ($values as $index => $value) {
                // get from current relation
                // if has child with same primary key, use this
                /* @var $newChild \yii\db\ActiveRecord */
                $newChild = null;
                if (empty($relation->indexBy)) {
                    foreach ($children as $i => $child) {
                        if ($this->isEqual($child, $value, $uniqueKeys)) {
                            if ($value instanceof $class) {
                                $newChild = $value;
                                $newChild->isNewRecord = $child->isNewRecord;
                                $newChild->oldAttributes = $child->oldAttributes;
                            } else {
                                $newChild = $child;
                            }
                            unset($children[$i]);
                            break;
                        }
                    }
                } elseif (isset($children[$index])) {
                    $child = $children[$index];
                    if ($value instanceof $class) {
                        $newChild = $value;
                        $newChild->isNewRecord = $child->isNewRecord;
                        $newChild->oldAttributes = $child->oldAttributes;
                    } else {
                        $newChild = $child;
                    }
                    unset($children[$index]);
                }
                if ($newChild === null) {
                    $newChild = $value instanceof $class ? $value : new $class;
                }
                if (isset($this->relatedScenarios[$name])) {
                    $newChild->scenario = $this->relatedScenarios[$name];
                }
                if (!$value instanceof $class) {
                    $newChild->load($value, '');
                }
                foreach ($link as $from => $to) {
                    $newChild->$from = $this->owner->$to;
                }
                $newChildren[$index] = $newChild;
            }
            $this->_old_relations[$name] = $children;
            $this->owner->populateRelation($name, $newChildren);
            $this->_process_relation[$name] = true;
        } else {
            $newChild = null;
            if ($children === null) {
                if ($values !== null) {
                    $newChild = $values instanceof $class ? $values : new $class;
                    $this->_process_relation[$name] = true;
                }
            } else {
                if ($values !== null) {
                    $newChild = $values instanceof $class ? $values : $children;
                    if ($values instanceof $class) {
                        $newChild = $values;
                        $newChild->oldAttributes = $children->oldAttributes;
                        $newChild->isNewRecord = $children->isNewRecord;
                    } else {
                        $newChild = $children;
                    }
                } else {
                    $this->_old_relations[$name] = [$children];
                }
                $this->_process_relation[$name] = true;
            }
            if ($newChild !== null) {
                if (isset($this->relatedScenarios[$name])) {
                    $newChild->scenario = $this->relatedScenarios[$name];
                }
                if (!$values instanceof $class) {
                    $newChild->load($values, '');
                }
                foreach ($link as $from => $to) {
                    $newChild->$from = $this->owner->$to;
                }
            }
            $this->owner->populateRelation($name, $newChild);
        }
        return true;
    }
238072
public function setFieldValues(array $values)
    {
        $fields = $this->toArray();
        foreach ($fields as $name => $value) {
            if (isset($values[$name]) && (!($this->getField($name) instanceof Element\Button) &&
                !($this->getField($name) instanceof Element\Input\Button) &&
                !($this->getField($name) instanceof Element\Input\Submit) &&
                !($this->getField($name) instanceof Element\Input\Reset))) {
                $this->setFieldValue($name, $values[$name]);
            } else if (!($this->getField($name) instanceof Element\Button) &&
                !($this->getField($name) instanceof Element\Input\Button) &&
                !($this->getField($name) instanceof Element\Input\Submit) &&
                !($this->getField($name) instanceof Element\Input\Reset)) {
                $this->getField($name)->resetValue();
            }
        }
        $this->filterValues();
        return $this;
    }
202742
public function loadHasRelations($name, $foreignKey, $callback = null, $storeKey = null)
    {
        $storeKey = $storeKey ? : $name;
        if (!isset($this->relations[$storeKey])) {
            $keys = array_unique($this->pluck($this->table->getPrimaryKey()));

            if (count($keys) > 0) {
                $relationRows = $this->table->similarTable($name);
                if (count($keys) === 1) {
                    $relationRows->where("`{$foreignKey}` = ?", $keys[0]);
                } else {
                    $relationRows->whereIn($foreignKey, $keys);
                }
                if ($callback) {
                    Coroutine::callUserFuncArray($callback, [$relationRows]);
                }
                $relationRows = $relationRows->rows();
            } else {
                $relationRows = new Rows([], $this->table->similarTable($name));
            }
            $this->relations[$storeKey] = $relationRows;
        }
        return $this->relations[$storeKey];
    }
124385
public function insert(array $values)
    {
        // Since every insert gets treated like a batch insert, we will make sure the
        // bindings are structured in a way that is convenient when building these
        // inserts statements by verifying these elements are actually an array.
        if (empty($values)) {
            return true;
        }

        if (! is_array(reset($values))) {
            $values = [$values];
        }

        // Here, we will sort the insert keys for every record so that each insert is
        // in the same order for the record. We need to make sure this is the case
        // so there are not any errors or problems when inserting these records.
        else {
            foreach ($values as $key => $value) {
                ksort($value);

                $values[$key] = $value;
            }
        }

        // Finally, we will run this query against the database connection and return
        // the results. We will need to also flatten these bindings before running
        // the query so they are all in one huge, flattened array for execution.
        $compiledInsert = $this->grammar->compileInsert($this, $values);

        return $this->connection->insert(
            $compiledInsert,
            $this->cleanBindings(Arr::flatten($values, 1))
        );
    }
698273
public function hasRelation($name)
    {
        $name = $this->prepareRelationName($name);

        return key_exists($name, $this->relations);
    }
952304
public function addUpdateValues($values)
    {
        $valuesSeq = [];
        foreach ($values as $name => $value) {
            $field = $this->model->getMeta()->getField($name);
            $model = $field->scopeModel->getMeta()->getConcreteModel();
            $isDirect = (!($field->autoCreated && !$field->concrete) || !$field->concrete);
            if (!$isDirect || ($field->isRelation && $field->manyToMany)) {
                throw new  FieldError(
                    sprintf(
                        'Cannot update model field %r (only non-relations and '.
                        'foreign keys permitted).',
                        $field
                    )
                );
            }
            if ($model->getMeta()->getModelName() !== $this->model->getMeta()->getModelName()) {
                $this->addRelatedUpdate($model, $field, $value);

                continue;
            }
            $valuesSeq[] = [$field, $model, $value];
        }

        return $this->addUpdateFields($valuesSeq);
    }
198006
public function insert(array $values = []): bool
	{
		$query = $this->compiler->insert($values);

		return $this->connection->query($query['sql'], $query['params']);
	}
1236587
public function loadRelations($data)
    {
        /** @var BaseActiveRecord $owner */
        $owner = $this->owner;
        foreach ($this->_relations as $relationName) {
            $keyName = $this->_getRelationKeyName($relationName);
            if (array_key_exists($keyName, $data)) {
                $owner->{$relationName} = $data[$keyName];
            }
        }
    }
1335953
public function add($names, $values = null, $attrib = array())
    {
        if (is_array($names)) {
            foreach ($names as $i => $text) {
                $this->options[] = array('text' => $text, 'value' => $values[$i]) + $attrib;
            }
        }
        else {
            $this->options[] = array('text' => $names, 'value' => $values) + $attrib;
        }
    }
414771
public static function set($name, $value)
    {
        $model = self::find()->where([self::getNameAttribute() => $name])->one();
    
        if ($model) {
            return (bool) $model->updateAttributes([
                self::getValueAttribute() => $value,
            ]);
        }
    
        $model = new self();
        $model->value = $value;
        $model->name = $name;
        return $model->save();
    }
378120
public function populateTree($depth = null, $with = null)
    {
        /** @var ActiveRecord[]|static[] $nodes */
        $query = $this->getDescendants($depth);
        if ($with) {
            $query->with($with);
        }
        $nodes = $query->all();

        $key = $this->owner->getAttribute($this->leftAttribute);
        $relates = [];
        $parents = [$key];
        $prev = $this->owner->getAttribute($this->depthAttribute);
        foreach($nodes as $node)
        {
            $level = $node->getAttribute($this->depthAttribute);
            if ($level <= $prev) {
                $parents = array_slice($parents, 0, $level - $prev - 1);
            }

            $key = end($parents);
            if (!isset($relates[$key])) {
                $relates[$key] = [];
            }
            $relates[$key][] = $node;

            $parents[] = $node->getAttribute($this->leftAttribute);
            $prev = $level;
        }

        $ownerDepth = $this->owner->getAttribute($this->depthAttribute);
        $nodes[] = $this->owner;
        foreach ($nodes as $node) {
            $key = $node->getAttribute($this->leftAttribute);
            if (isset($relates[$key])) {
                $node->populateRelation('children', $relates[$key]);
            } elseif ($depth === null || $ownerDepth + $depth > $node->getAttribute($this->depthAttribute)) {
                $node->populateRelation('children', []);
            }
        }

        return $this->owner;
    }
700576
public function allowedValues(string $name, array $values): bool
    {
        if (!$this->exists($name)) {
            throw new EnvironmentException(\sprintf('Variable %s doesn\'t exist', $name));
        }

        return \in_array($name, $values, true);
    }
844181
public function loadValues(array $values)
    {
        foreach ($values as $name => $value) {
            $argument = $this->getArgument($name, static::MODE_RETURN_NULL);

            if (null !== $argument) {
                $argument->setValue($value);
            }
        }

        return $this;
    }
758771
public function setRelation($name, $value, $data = [])
    {
        // 检测修改器
        $method = 'set' . Db::parseName($name, 1) . 'Attr';

        if (method_exists($this, $method)) {
            $value = $this->$method($value, array_merge($this->data, $data));
        }

        $this->relation[$name] = $value;

        return $this;
    }
960117
protected static function _relations($type = null, $name = null) {
		$self = static::_object();

		if ($name) {
			if (isset($self->_relationsToLoad[$name])) {
				$t = $self->_relationsToLoad[$name];
				unset($self->_relationsToLoad[$name]);
				return static::bind($t, $name, (array) $self->{$t}[$name]);
			}
			return isset($self->_relations[$name]) ? $self->_relations[$name] : null;
		}
		if (!$type) {
			foreach ($self->_relationsToLoad as $name => $t) {
				static::bind($t, $name, (array) $self->{$t}[$name]);
			}
			$self->_relationsToLoad = [];
			return $self->_relations;
		}
		foreach ($self->_relationsToLoad as $name => $t) {
			if ($type === $t) {
				static::bind($t, $name, (array) $self->{$t}[$name]);
				unset($self->_relationsToLoad[$name]);
			}
		}
		return array_filter($self->_relations, function($i) use ($type) {
			return $i->data('type') === $type;
		});
	}
1303949
protected function checkRelationExistence($name)
    {
        $getter = 'get' . ucfirst($name);
        $class = get_class($this->owner);

        if (method_exists($this->owner, $getter)) {
            /** @var ActiveQuery $value */
            $value = $this->owner->$getter();

            if (!($value instanceof ActiveQueryInterface)) {
                throw new InvalidConfigException("Value of relation '$getter' not valid");
            }

            $this->models[$name]['multiple'] = $value->multiple;
        } else {
            throw new InvalidConfigException("Relation '$class::$getter' for attribute '$name' does not exists");
        }
    }
213590
public function update(array $values)
	{
		$bindings = array_values(array_merge($values, $this->bindings));

		$sql = $this->grammar->compileUpdate($this, $values);

		return $this->connection->update($sql, $this->cleanBindings($bindings));
	}