当前位置: 首页>>代码示例>>PHP>>正文


PHP ActiveRecord::populateRelation方法代码示例

本文整理汇总了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);
 }
开发者ID:Liv1020,项目名称:cms,代码行数:12,代码来源:RelationalBehavior.php

示例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);
 }
开发者ID:e96,项目名称:yii2-relational-behavior,代码行数:17,代码来源:RelationalBehavior.php

示例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);
 }
开发者ID:sonrac,项目名称:yii2-file-kit,代码行数:18,代码来源:UploadBehavior.php

示例4: load

 /**
  * @inheritdoc
  */
 public function load($data)
 {
     $this->_model = $this->getMeta();
     $this->owner->populateRelation('metaRelation', $this->_model);
     return $this->_model->load($data);
 }
开发者ID:manyoubaby123,项目名称:imshop,代码行数:9,代码来源:SeoBehavior.php

示例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);
     }
 }
开发者ID:portalsway2,项目名称:APEDevices,代码行数:66,代码来源:Model.php


注:本文中的yii\db\ActiveRecord::populateRelation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。