本文整理汇总了PHP中yii\db\ActiveRecord::populateRelation方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::populateRelation方法的具体用法?PHP ActiveRecord::populateRelation怎么用?PHP ActiveRecord::populateRelation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::populateRelation方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __set
public function __set($name, $value)
{
if (is_array($value) && count($value) > 0 && !$value[0] instanceof Object || !is_array($value) && !$value instanceof Object) {
$getter = 'get' . $name;
/** @var ActiveQuery $query */
$query = $this->owner->{$getter}();
/* @var $modelClass ActiveRecord */
$modelClass = $query->modelClass;
$value = $modelClass::findAll($value);
}
$this->owner->populateRelation($name, $value);
}
示例2: __set
public function __set($name, $value)
{
if (is_array($value) && count($value) > 0 && !$value[0] instanceof Object || !is_array($value) && !$value instanceof Object) {
$getter = 'get' . $name;
/** @var ActiveQuery $activeQuery */
$activeQuery = $this->owner->{$getter}();
/* @var $modelClass ActiveRecord */
$modelClass = $activeQuery->modelClass;
$value = $modelClass::findAll($value);
if (!empty($activeQuery->via)) {
list($junctionTable, $primaryModelColumn, $relatedModelColumn) = $this->parseQuery($activeQuery);
$query = new Query(['select' => [$relatedModelColumn], 'from' => [$junctionTable], 'where' => [$primaryModelColumn => $this->owner->primaryKey]]);
$this->oldRelations[$name] = $query->createCommand()->queryColumn();
}
}
$this->owner->populateRelation($name, $value);
}
示例3: saveFilesToRelation
/**
* @param array $files
*/
protected function saveFilesToRelation($files)
{
$modelClass = $this->getUploadModelClass();
$models = [];
foreach ($files as $name => $file) {
$model = new $modelClass();
$model->setScenario($this->uploadModelScenario);
$model = $this->loadModel($model, $file);
if ($this->getUploadRelation()->via !== null) {
$model->save(false);
}
$models[$name] = $model;
}
$this->owner->populateRelation($this->uploadRelation, $models);
}
示例4: load
/**
* @inheritdoc
*/
public function load($data)
{
$this->_model = $this->getMeta();
$this->owner->populateRelation('metaRelation', $this->_model);
return $this->_model->load($data);
}
示例5: populateRelation
/**
* @inheritdoc
*/
public function populateRelation($name, $value)
{
/** @var Model $related */
/** @var Model $relatedRecord */
$relation = $this->getRelation($name);
$relatedRecordClass = $relation->modelClass;
if ($relation->multiple) {
$relatedRecords = array();
$this->_oldRelated[$name] = $this->{$name};
foreach ($value as $recordData) {
$related = null;
if (isset($recordData['id'])) {
$related = $relatedRecordClass::findOne($recordData['id']);
}
if (!$related or !$related instanceof Model) {
$related = new $relatedRecordClass();
}
$related->load($recordData, '');
$relatedRecords[] = $related;
}
parent::populateRelation($name, $relatedRecords);
/**
* @param Model $oldRelatedRecord
* @param integer $key
* @param Model $args
*/
$deleteIfNotExitsInNewRelatedList = function ($oldRelatedRecord, $key, $args) {
$exists = false;
$relatedRecords = $args[0];
/** @var ActiveQuery $relation */
$relation = $args[1];
foreach ($relatedRecords as $relatedRecord) {
if ($relatedRecord->isNewRecord) {
continue;
}
if ($oldRelatedRecord->equals($relatedRecord)) {
$exists = true;
}
}
if ($relation->via instanceof ActiveQueryInterface) {
$id = array_keys($relation->via->link);
$id = reset($id);
$viaTable = reset($relation->via->from);
static::getDb()->createCommand()->delete($viaTable, [$id => $this->id])->execute();
} elseif (!$exists) {
//old related record doesn't exists in the updated list so it has to be deleted
$oldRelatedRecord->delete();
}
};
$args = [$relatedRecords, $relation];
array_walk($this->_oldRelated[$name], $deleteIfNotExitsInNewRelatedList, $args);
} else {
$relatedRecord = new $relatedRecordClass();
if (isset($value['id'])) {
$relatedRecord = $relatedRecordClass::findOne($value['id']);
}
if (!$relatedRecord or !$relatedRecord instanceof Model) {
$relatedRecord = new $relatedRecordClass();
}
$relatedRecord->load($value, '');
parent::populateRelation($name, $relatedRecord);
}
}