當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。