php yii-db-ActiveRecord类(方法)实例源码

下面列出了php yii-db-ActiveRecord 类(方法)源码代码实例,从而了解它的用法。

作者:sangki    项目:applicatio   
/**
  * 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;
 }

作者:omniligh    项目:yz2-admi   
/**
  * @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);
     }
 }

作者:vtv    项目:yii2-relation   
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']);
 }

作者:cubicla    项目:project-cub   
/**
  * @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;
     }
 }

作者:nisnake    项目:yii2-templat   
protected function _delete(ActiveRecord $model)
 {
     $id = Yii::$app->request->post('id');
     $model->findOne($id)->delete();
     //        $model::deleteAll($id);
     return $this->_success();
 }

作者:vladdnep    项目:yii2-ycm-util   
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);
 }

作者:felixmaier198    项目:yii2-renderdua   
/**
  * @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');
 }

作者:pbabila    项目:bcod   
/**
  * @param ActiveRecord $model
  */
 public function addErrorMessagesFromModel(ActiveRecord $model)
 {
     foreach ($model->getErrors() as $fieldWithErrors) {
         foreach ($fieldWithErrors as $error) {
             $this->addMessage(null, $error, Message::ALERT);
         }
     }
 }

作者:Wubbleyo    项目:yii2-ordermode   
/**
  * @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;
 }

作者:VEKsoftwar    项目:yii2-log-behavio   
/**
  * @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);
 }

作者:voodoo-mobil    项目:yii2-uploa   
/**
  * @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;
 }

作者:yiisof    项目:yii2-shel   
/**
  * 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;
 }

作者:sangkilsof    项目:sangkilbiz-   
/**
  * 
  * @param ActiveRecord $model
  * @param type $key
  * @param type $index
  * @return type
  */
 protected function renderDataCellContent($model, $key, $index)
 {
     $result = Html::tag('span', $index + 1, ['class' => 'serial-column']);
     if ($model instanceof ActiveRecord && ($primaryKeys = $model->primaryKey()) != []) {
         foreach ($primaryKeys as $primary) {
             $result .= ' ' . Html::activeHiddenInput($model, "[{$index}]{$primary}");
         }
     }
     return $result;
 }

作者:zeleni    项目:yii2-slug-behavio   
/**
  * @inheritdoc
  * @param ActiveRecord $owner
  */
 public function attach($owner)
 {
     $this->attribute = (array) $this->attribute;
     $primaryKey = $owner->primaryKey();
     $primaryKey = is_array($primaryKey) ? array_shift($primaryKey) : $primaryKey;
     if (in_array($primaryKey, $this->attribute, true) && $owner->getIsNewRecord()) {
         $this->attributes[ActiveRecord::EVENT_AFTER_INSERT] = $this->slugAttribute;
     }
     parent::attach($owner);
 }

作者:xiaohongyan    项目:yii_sho   
/**
 * update count column
 * @param ActiveRecord $oneData
 * @param $filedArray     update column array
 * @param $step           update count number or update count number array
 * e.g
        $obj = GuestBook::findOne(3);
        $this->_update_data_count($obj, ['up_time'], 1);
 */
 public function _update_data_count(ActiveRecord $oneData, $filedArray, $step)
 {
     $arr = array();
     if (is_array($filedArray) && count($filedArray)) {
         foreach ($filedArray as $_k => $_v) {
             $arr[$_v] = is_array($step) ? $step[$_k] : $step;
         }
     }
     $oneData->updateCounters($arr);
 }

作者:vastande    项目:yii2-ee   
public function attributeValue(ActiveRecord $model, $fieldName)
 {
     $complexName = $model->tableName() . '.' . $fieldName;
     $decode = \Yii::$app->params['decode'];
     $key = $model->{$fieldName};
     if ($key !== null && isset($decode[$complexName])) {
         return $decode[$complexName][$key];
     } else {
         return $key;
     }
 }

作者:highestgoodlikewate    项目:yii2-toolbo   
/**
  * @inheritdoc
  * @param \yii\db\ActiveRecord $owner
  */
 public function attach($owner)
 {
     //assert owner extends class ActiveRecord
     if (!$owner instanceof ActiveRecord) {
         throw new InvalidConfigException('ArchiveBehavior can only be applied to classes extending \\yii\\db\\ActiveRecord');
     }
     if ($owner->tableSchema->getColumn($this->archiveAttribute) === null) {
         throw new InvalidConfigException(sprintf('The table %s does not contain a column named %s', $owner->tableName(), $this->archiveAttribute));
     }
     parent::attach($owner);
 }

作者:vladdnep    项目:yii2-ycm-util   
/**
  * Get available relation choices
  * @param $relation_name
  * @return mixed
  */
 public static function getSelectChoices(ActiveRecord $model, $relation_name)
 {
     $class = $model->className();
     if (!isset(self::$relationsChoices[$class][$relation_name])) {
         self::$relationsChoices[$class][$relation_name] = [];
         $relation = $model->getRelation($relation_name, false);
         if ($relation) {
             self::$relationsChoices[$class][$relation_name] = ModelHelper::getSelectChoices(new $relation->modelClass());
         }
     }
     return self::$relationsChoices[$class][$relation_name];
 }

作者:Liv102    项目:cm   
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);
 }

作者:rocketyan    项目:hasscms-ap   
/**
  *
  * @param \yii\db\ActiveRecord $model
  */
 public function getQuery($model)
 {
     $query = $model->search($this->filters);
     foreach ($this->query as $key => $value) {
         call_user_func([$query, $key], $value);
     }
     /**
      * sort过滤
      */
     $query->addOrderBy($this->sort->orders);
     return $query;
 }


问题


面经


文章

微信
公众号

扫码关注公众号