當前位置: 首頁>>代碼示例>>PHP>>正文


PHP db\ActiveRecord類代碼示例

本文整理匯總了PHP中yii\db\ActiveRecord的典型用法代碼示例。如果您正苦於以下問題:PHP ActiveRecord類的具體用法?PHP ActiveRecord怎麽用?PHP ActiveRecord使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ActiveRecord類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: processOutput

 /**
  * Process output. Throw error when not success with unknown reason.
  * @param boolean      $success
  * @param ActiveRecord $model
  *
  * @return ActiveRecord
  * @throws \yii\web\ServerErrorHttpException
  */
 protected static function processOutput($success, $model)
 {
     if (!$success && !$model->hasErrors()) {
         throw new ServerErrorHttpException('Error with unknown reason.');
     }
     return $model;
 }
開發者ID:sangkil,項目名稱:application,代碼行數:15,代碼來源:Api.php

示例2: getCreateUpdateResponse

 /**
  * @param ActiveRecord $model
  * @param array $actions Custom actions array in the form of
  * ```php
  * 'name' => function() {
  *
  * }
  * ```
  * @param bool $addDefaultActions If true default actions will be added
  * @return \yii\web\Response
  * @throws BadRequestHttpException
  */
 protected function getCreateUpdateResponse($model, $actions = [], $addDefaultActions = true)
 {
     $defaultActions = [AdminHtml::ACTION_SAVE_AND_STAY => function () use($model) {
         /** @var Controller | CrudControllerTrait $this */
         return $this->redirect(['update', 'id' => $model->getPrimaryKey()]);
     }, AdminHtml::ACTION_SAVE_AND_CREATE => function () use($model) {
         /** @var Controller | CrudControllerTrait $this */
         if ($url = Url::previous($this->createUrlParam)) {
             Url::remember(null, $this->createUrlParam);
             return $this->redirect($url);
         }
         return $this->redirect(['create']);
     }, AdminHtml::ACTION_SAVE_AND_LEAVE => function () use($model) {
         /** @var Controller | CrudControllerTrait $this */
         if ($url = \Yii::$app->request->get('return')) {
             return $this->redirect($url);
         }
         if ($url = Url::previous($this->indexUrlParam)) {
             Url::remember(null, $this->indexUrlParam);
             return $this->redirect($url);
         }
         return $this->redirect(['index']);
     }];
     if ($addDefaultActions) {
         $actions = array_merge($defaultActions, $actions);
     }
     $actionName = \Yii::$app->request->post(AdminHtml::ACTION_BUTTON_NAME, AdminHtml::ACTION_SAVE_AND_LEAVE);
     if (isset($actions[$actionName])) {
         return call_user_func($actions[$actionName]);
     } else {
         throw new BadRequestHttpException('Unknown action: ' . $actionName);
     }
 }
開發者ID:omnilight,項目名稱:yz2-admin,代碼行數:45,代碼來源:CrudControllerTrait.php

示例3: init

 public function init()
 {
     parent::init();
     $this->owner->on(ActiveRecord::EVENT_AFTER_INSERT, [$this, 'save']);
     $this->owner->on(ActiveRecord::EVENT_AFTER_UPDATE, [$this, 'save']);
     $this->owner->on(ActiveRecord::EVENT_BEFORE_DELETE, [$this, 'unlink']);
 }
開發者ID:vtvz,項目名稱:yii2-relations,代碼行數:7,代碼來源:BaseRelation.php

示例4: run

 /**
  * @param integer $type
  * @param \yii\db\ActiveRecord $object
  */
 public function run($type, $object)
 {
     $pkey = $object->primaryKey();
     $pkey = $pkey[0];
     $data = ['table' => $object->tableName(true), 'model_id' => $object->getPrimaryKey(), 'type' => $type, 'date' => date('Y-m-d H:i:s', time())];
     switch ($type) {
         case self::EVT_INSERT:
             $data['field_name'] = $pkey;
             $this->saveField($data);
             break;
         case self::EVT_UPDATE:
             foreach ($this->updatedFields as $updatedFieldKey => $updatedFieldValue) {
                 $data['field_name'] = $updatedFieldKey;
                 $data['old_value'] = $updatedFieldValue;
                 $data['new_value'] = $object->{$updatedFieldKey};
                 $this->saveField($data);
             }
             break;
         case self::EVT_DELETE:
             $data['field_name'] = $pkey;
             $this->saveField($data);
             break;
         case self::EVT_UPDATE_PK:
             $data['field_name'] = $pkey;
             $data['old_value'] = $object->getOldPrimaryKey();
             $data['new_value'] = $object->{$pkey};
             $this->saveField($data);
             break;
     }
 }
開發者ID:cubiclab,項目名稱:project-cube,代碼行數:34,代碼來源:ChangedocManager.php

示例5: _delete

 protected function _delete(ActiveRecord $model)
 {
     $id = Yii::$app->request->post('id');
     $model->findOne($id)->delete();
     //        $model::deleteAll($id);
     return $this->_success();
 }
開發者ID:nisnaker,項目名稱:yii2-template,代碼行數:7,代碼來源:Base.php

示例6: relation

 public static function relation(ActiveRecord $model, $relation_name, $options = [])
 {
     /* @var ActiveRecord|YcmModelUtilTrait $model */
     $relation = $model->getRelation($relation_name);
     $config = [$relation_name, 'widget', 'widgetClass' => Select2::className(), 'data' => RelationHelper::getSelectChoices($model, $relation_name), 'hideSearch' => false, 'options' => ['multiple' => $relation->multiple, 'placeholder' => 'Select...'], 'pluginOptions' => ['allowClear' => true]];
     return ArrayHelper::merge($config, $options);
 }
開發者ID:vladdnepr,項目名稱:yii2-ycm-utils,代碼行數:7,代碼來源:EditHelper.php

示例7: testRenderDualNotController

 /**
  * @test
  */
 public function testRenderDualNotController()
 {
     $controller = new \yii\db\ActiveRecord();
     $controller->attachBehavior('RenderDual', new \yii2renderdual\RenderDual());
     $this->setExpectedException('Exception');
     $render = $controller->renderDual('view', ['foo' => 'bar'], 'foo bar');
 }
開發者ID:felixmaier1989,項目名稱:yii2-renderdual,代碼行數:10,代碼來源:RenderDualTest.php

示例8: addErrorMessagesFromModel

 /**
  * @param ActiveRecord $model
  */
 public function addErrorMessagesFromModel(ActiveRecord $model)
 {
     foreach ($model->getErrors() as $fieldWithErrors) {
         foreach ($fieldWithErrors as $error) {
             $this->addMessage(null, $error, Message::ALERT);
         }
     }
 }
開發者ID:pbabilas,項目名稱:bcode,代碼行數:11,代碼來源:AbstractController.php

示例9: getUrl

 /**
  * @param $direction string
  * @param $model ActiveRecord
  * @return array
  */
 protected function getUrl($direction, ActiveRecord $model)
 {
     $url = !empty($this->url) ? $this->url : ['order'];
     $url['direction'] = $direction;
     $url['attribute'] = $this->attribute;
     $url['id'] = $model->getPrimaryKey();
     return $url;
 }
開發者ID:Wubbleyou,項目名稱:yii2-ordermodel,代碼行數:13,代碼來源:OrderModelColumn.php

示例10: attach

 /**
  * @inheritdoc
  *
  * @param ActiveRecord $owner
  *
  * @throws ErrorException
  */
 public function attach($owner)
 {
     if (!self::$_eventSwitched) {
         Event::on($owner->className(), VekActiveRecord::EVENT_TO_SAVE_MULTIPLE, [self::className(), 'logToSaveMultiple']);
         Event::on($owner->className(), VekActiveRecord::EVENT_SAVED_MULTIPLE, [self::className(), 'logSavedMultiple']);
     }
     parent::attach($owner);
 }
開發者ID:VEKsoftware,項目名稱:yii2-log-behavior,代碼行數:15,代碼來源:MultipleLog.php

示例11: getFilenameFor

 /**
  * @param  ActiveRecord $activeRecord
  * @param               $attribute
  * @param null $extension
  *
  * @return string
  */
 public function getFilenameFor($activeRecord, $attribute, $extension = null)
 {
     $path = Inflector::camel2id((new \ReflectionClass($activeRecord))->getShortName());
     $basename = implode('-', $activeRecord->getPrimaryKey(true)) . '-' . $attribute;
     if ($extension) {
         $basename .= '.' . $extension;
     }
     return $path . DIRECTORY_SEPARATOR . $basename;
 }
開發者ID:voodoo-mobile,項目名稱:yii2-upload,代碼行數:16,代碼來源:Writer.php

示例12: castModel

 /**
  * Get an array representing the properties of a model.
  *
  * @param \yii\db\ActiveRecord $model
  * @return array
  */
 public static function castModel(ActiveRecord $model)
 {
     $attributes = array_merge($model->getAttributes(), $model->getRelatedRecords());
     $results = [];
     foreach ($attributes as $key => $value) {
         $results[Caster::PREFIX_VIRTUAL . $key] = $value;
     }
     return $results;
 }
開發者ID:yiisoft,項目名稱:yii2-shell,代碼行數:15,代碼來源:YiiCaster.php

示例13: findOrderNum

 /**
  * Find and set order numeric
  */
 public function findOrderNum()
 {
     if (!$this->owner->{$this->attribute}) {
         $maxOrderNum = (int) (new Query())->select("MAX({$this->attribute})")->from($this->owner->tableName())->scalar();
         $this->owner->{$this->attribute} = $maxOrderNum + $this->step;
     }
 }
開發者ID:roboapp,項目名稱:base,代碼行數:10,代碼來源:SortableBehavior.php

示例14: init

 public function init()
 {
     $this->id = $this->model->formName();
     $this->action = RequestModule::getPopupUrl(['type' => $this->model->tableName()]);
     $this->fieldConfig = ['template' => '<div class="row">{input}{error}</div>', 'errorOptions' => ['class' => 'errorMessage']];
     parent::init();
 }
開發者ID:tolik505,項目名稱:bl,代碼行數:7,代碼來源:CustomForm.php

示例15: run

 public function run($action)
 {
     $this->type = Yii::$app->request->get('type');
     $this->behaviorName = Yii::$app->request->get('behaviorName');
     $this->galleryId = Yii::$app->request->get('galleryId');
     $pkNames = call_user_func([$this->types[$this->type], 'primaryKey']);
     $pkValues = explode($this->pkGlue, $this->galleryId);
     $pk = array_combine($pkNames, $pkValues);
     $this->owner = call_user_func([$this->types[$this->type], 'findOne'], $pk);
     $this->behavior = $this->owner->getBehavior($this->behaviorName);
     switch ($action) {
         case 'delete':
             return $this->actionDelete(Yii::$app->request->post('id'));
             break;
         case 'ajaxUpload':
             return $this->actionAjaxUpload();
             break;
         case 'changeData':
             return $this->actionChangeData(Yii::$app->request->post('photo'));
             break;
         case 'order':
             return $this->actionOrder(Yii::$app->request->post('order'));
             break;
         default:
             throw new HttpException(400, 'Action do not exists');
             break;
     }
 }
開發者ID:tamvodopad,項目名稱:yii2-gallery-manager,代碼行數:28,代碼來源:GalleryManagerAction.php


注:本文中的yii\db\ActiveRecord類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。