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


PHP ActiveRecord::link方法代码示例

本文整理汇总了PHP中yii\db\ActiveRecord::link方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::link方法的具体用法?PHP ActiveRecord::link怎么用?PHP ActiveRecord::link使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yii\db\ActiveRecord的用法示例。


在下文中一共展示了ActiveRecord::link方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: afterInsert

 /**
  * Handles afterInsert event of the owner.
  */
 public function afterInsert()
 {
     if ($this->_model) {
         if ($this->ownerType) {
             $this->_model->entity_type = $this->ownerType;
         }
         $this->owner->link('metaRelation', $this->_model);
     }
 }
开发者ID:manyoubaby123,项目名称:imshop,代码行数:12,代码来源:SeoBehavior.php

示例2: saveFilesToRelation

 /**
  * @param array $files
  */
 protected function saveFilesToRelation($files)
 {
     $modelClass = $this->getUploadModelClass();
     foreach ($files as $file) {
         $model = new $modelClass();
         $model->setScenario($this->uploadModelScenario);
         $model = $this->loadModel($model, $file);
         if ($this->getUploadRelation()->via !== null) {
             $model->save(false);
         }
         $this->owner->link($this->uploadRelation, $model);
     }
 }
开发者ID:yiioverflow,项目名称:yii2-file-kit,代码行数:16,代码来源:UploadBehavior.php

示例3: afterSave

 public function afterSave()
 {
     $data = [];
     if (isset($_POST[$this->owner->formName()])) {
         $data = $_POST[$this->owner->formName()];
     }
     if ($data) {
         foreach ($data as $attribute => $value) {
             if (!in_array($attribute, $this->relations)) {
                 continue;
             }
             if (is_array($value)) {
                 $relation = $this->owner->getRelation($attribute);
                 if ($relation->via !== null) {
                     /** @var ActiveRecord $foreignModel */
                     $foreignModel = $relation->modelClass;
                     $this->owner->unlinkAll($attribute, true);
                     if (is_array(current($value))) {
                         foreach ($value as $data) {
                             if (isset($data[$foreignModel::primaryKey()[0]]) && $data[$foreignModel::primaryKey()[0]] > 0) {
                                 $fm = $foreignModel::findOne($data[$foreignModel::primaryKey()[0]]);
                                 $fm->load($data, '');
                                 $this->owner->link($attribute, $fm);
                             }
                         }
                     } else {
                         foreach ($value as $fk) {
                             if (preg_match('~^\\d+$~', $fk)) {
                                 $this->owner->link($attribute, $foreignModel::findOne($fk));
                             }
                         }
                     }
                 }
             } else {
                 $this->owner->unlinkAll($attribute, true);
             }
         }
     }
 }
开发者ID:diiimonn,项目名称:yii2-behavior-relation-follow,代码行数:39,代码来源:RelationFollowBehavior.php

示例4: linkAll

 public static function linkAll(ActiveRecord $model, array $models, $relationName)
 {
     try {
         foreach ($models as $item) {
             if ($item->isNewRecord) {
                 $model->link($relationName, $item);
             } else {
                 $item->save();
             }
         }
         return true;
     } catch (Exception $e) {
         return false;
     }
 }
开发者ID:ninetor,项目名称:yii-classifield,代码行数:15,代码来源:Model.php

示例5: saveRelationModel

 /**
  * Saves a single model within a relation.
  * via: 			item is saved, then linked
  * direct link:		item is saved by linking
  * existing models:	
  * @param  ActiveRecord $model  	base model
  * @param  string $relationName		name of the relation
  * @param  boolean $isVia        	relation via table
  * @param  ActiveRecord $item       the model in the relation
  * @param  array $config       		configuration array
  * @return boolean               	save success
  * @internal
  */
 private function saveRelationModel(&$model, $relationName, $isVia, &$item, &$config)
 {
     $result = true;
     $skipUpdate = ArrayHelper::getValue($config, self::SKIP_UPDATE, $this->defaultSkipUpdate);
     if ($isVia) {
         if ($item->isNewRecord) {
             $result = $result && $item->save();
             $model->link($relationName, $item);
         } else {
             if (!$skipUpdate) {
                 $result = $result && $item->save();
             }
             $findInRelation = $model->getRelation($relationName)->andWhere($item->getPrimaryKey(true))->exists();
             if (!$findInRelation) {
                 $model->link($relationName, $item);
             }
         }
     } else {
         if ($item->isNewRecord) {
             $model->link($relationName, $item);
         } else {
             if (!$skipUpdate) {
                 // save linked item only, if something changed
                 if (count($item->getDirtyAttributes()) > 0) {
                     $item->save();
                 }
             }
         }
     }
     $result = $result && $this->saveRelations($item, $config);
     return $result;
 }
开发者ID:e-frank,项目名称:yii2-data,代码行数:45,代码来源:ActiveDocumentBehavior.php


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