php yii-base-Model类(方法)实例源码

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

作者:ivan-chk    项目:yii2-boos   
/**
  * @param Model $model
  * @param string $message
  * @param int $code
  * @param Exception $previous
  */
 public function __construct(Model $model, $message = null, $code = 0, Exception $previous = null)
 {
     $this->model = $model;
     if (is_null($message) && $model->hasErrors()) {
         $message = implode(' ', array_map(function ($errors) {
             return implode(' ', $errors);
         }, $model->getErrors()));
     }
     parent::__construct($message, $code, $previous);
 }

作者:ostashevd    项目:bric   
/**
  * Performs ajax validation.
  *
  * @param \yii\base\Model $model
  *
  * @throws \yii\base\ExitException
  */
 protected function performValidationModel(Model $model)
 {
     if ($model->load(Yii::$app->request->post())) {
         return ActiveForm::validate($model);
     }
     return;
 }

作者:softa    项目:yii-use   
/**
  * Performs ajax validation.
  * @param Model $model
  * @throws \yii\base\ExitException
  */
 protected function performAjaxValidation(Model $model)
 {
     if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {
         \Yii::$app->response->format = Response::FORMAT_JSON;
         echo json_encode(ActiveForm::validate($model));
         \Yii::$app->end();
     }
 }

作者:CAPITALO    项目:capitalo   
/**
  * Берет значения из POST и возвраает знаяения для добавления в БД
  *
  * @param array           $field
  * @param \yii\base\Model $model
  *
  * @return array
  */
 public static function onLoad($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $value = ArrayHelper::getValue(\Yii::$app->request->post(), $model->formName() . '.' . $fieldName, false);
     if ($value) {
         $value = true;
     }
     $model->{$fieldName} = $value;
 }

作者:sydorenkov    项目:yiiad   
/**
  * server validatiom for unique zip_code
  * @param \yii\base\Model $model
  * @param string $attribute
  */
 public function validateAttribute($model, $attribute)
 {
     $value = $model->{$attribute};
     if (!Locations::find()->where(['zip_code' => $value])->exists()) {
         $model->addError($attribute, $this->message);
     }
 }

作者:ivan-chk    项目:yii2-kladovk   
public static function modelErrors(Model $model, $message = null, $category = 'application')
 {
     if (!is_null($message)) {
         static::error($message, $category);
     }
     if ($model->hasErrors()) {
         static::error(['class' => get_class($model), 'attributes' => $model->getAttributes(), 'errors' => $model->getErrors()], $category);
     }
 }

作者:yii2tec    项目:admi   
/**
  * Populates the model with input data.
  * @param Model $model model instance.
  * @param array $data the data array to load, typically `$_POST` or `$_GET`.
  * @return boolean whether expected forms are found in `$data`.
  */
 protected function load($model, $data)
 {
     /* @var $this Action */
     $event = new ActionEvent($this, ['model' => $model, 'result' => $model->load($data)]);
     $this->trigger('afterDataLoad', $event);
     return $event->result;
 }

作者:weblemen    项目:yii   
public function getHotlinkTo(Model $model, $action = null, $options = [])
 {
     if (!ArrayHelper::isIn($this->className(), ArrayHelper::getColumn($model->behaviors(), 'class'))) {
         throw new InvalidRouteException('The "LinkableBehavior" is not attached to the specified model');
     }
     return $this->getHotlink(strtr('{route}/{action}', ['{route}' => $model->route, '{action}' => $action ?? $model->defaultAction]), $model->linkableParams, $options);
 }

作者:netis-p    项目:yii2-cru   
/**
  * Retrieves grid columns configuration using the modelClass.
  * @param Model $model
  * @param array $fields
  * @return array grid columns
  */
 public function getIndexGridColumns($model, $fields)
 {
     $id = Yii::$app->request->getQueryParam('id');
     $relationName = Yii::$app->request->getQueryParam('relation');
     $multiple = Yii::$app->request->getQueryParam('multiple', 'true') === 'true';
     foreach ($fields as $key => $field) {
         if ((is_array($field) || !is_string($field) && is_callable($field)) && $key === $relationName || $field === $relationName) {
             unset($fields[$key]);
         }
     }
     return array_merge([['class' => 'yii\\grid\\CheckboxColumn', 'multiple' => $multiple, 'headerOptions' => ['class' => 'column-serial'], 'checkboxOptions' => function ($model, $key, $index, $column) use($id, $relationName) {
         /** @var \yii\db\ActiveRecord $model */
         $options = ['value' => is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key];
         if (empty($relationName) || $model->{$relationName} === null || trim($id) === '') {
             return $options;
         }
         $relation = $model->getRelation($relationName);
         if ($relation->multiple) {
             /** @var \yii\db\ActiveRecord $relationClass */
             $relationClass = $relation->modelClass;
             if (Action::importKey($relationClass::primaryKey(), $id) === $model->getAttributes(array_keys($relation->link))) {
                 $options['checked'] = true;
                 $options['disabled'] = true;
             }
         } elseif (Action::exportKey($relation->one()->getPrimaryKey()) === $id) {
             $options['checked'] = true;
             $options['disabled'] = true;
         }
         return $options;
     }], ['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'column-serial']]], self::getGridColumns($model, $fields));
 }

作者:dextercoo    项目:yii2-easyu   
/**
  * Serializes the validation errors in a model.
  * @param Model $model
  * @return array the array representation of the errors
  */
 protected function serializeModelErrors($model)
 {
     Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
     $result = [];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result[] = ['field' => $name, 'message' => $message];
     }
     return $result;
 }

作者:avrahamichle    项目:Highne   
public static function checkRequiredParams($params, $required_params)
 {
     $missing_parameters = self::allTheRequiredParametersAre($params, $required_params);
     $model = new Model();
     if ($missing_parameters) {
         $model->addError("this parameters are missing ", $error = implode(", ", $missing_parameters));
     }
     return $model;
 }

作者:hiqde    项目:hipanel-cor   
/**
  * @inheritdoc
  */
 public function set(Model $model)
 {
     if (Yii::$app->user->getIsGuest()) {
         return;
     }
     $data = $model->toArray();
     $this->getStorage()->setBounded('theme', $data);
     $this->setToCache($data);
 }

作者:undefinedstudi    项目:yii2-angular-for   
/**
  * Applies the required formatting and params to messages.
  * @param Model $model
  * @param string $attribute
  * @return array Prepared messages
  */
 public function prepareMessages($model, $attribute)
 {
     $params = array_merge($this->messageParams(), ['attribute' => $model->getAttributeLabel($attribute)]);
     $messages = [];
     foreach ($this->messages() as $name => $message) {
         $messages[$name] = Yii::$app->getI18n()->format($message, $params, Yii::$app->language);
     }
     return $messages;
 }

作者:sjoor    项目:yii2-component   
/**
  * Extracts Model errors from array and puts them into string
  * @param Model $model
  * @param boolean $html
  * @return string
  */
 public static function errorsToString($model, $html = true)
 {
     $result = '';
     $delimiter = $html ? "<br/>\n" : "\n";
     foreach ($model->errors as $attribute => $error) {
         $errors = implode(', ', $error);
         $result .= $model->getAttributeLabel($attribute) . ": {$errors}{$delimiter}";
     }
     return $result;
 }

作者:romka-che    项目:yii2-images-modul   
/**
  * @param \yii\base\Model $model
  *
  * @return string
  */
 public function getModelDirectory(Model $model)
 {
     if (!$model->canGetProperty('idAttribute')) {
         throw new \LogicException("Model {$model->className()} has not 'idAttribute' property");
     }
     $modelName = $this->getShortClass($model);
     /** @noinspection PhpUndefinedFieldInspection */
     $modelId = $model->{$model->idAttribute};
     return $this->_getSubDirectory($modelName, $modelId);
 }

作者:cranky    项目:change-log-behavio   
/**
  * @param \yii\base\Model $model
  *
  * @return string
  */
 protected function getCategory(Model $model)
 {
     if (!$model->isNewRecord) {
         $id = $model->id;
         $category = self::className() . ':' . $model->formName() . '-' . $id;
     } else {
         $category = self::className() . ':' . $model->formName();
     }
     return $category;
 }

作者:epoxxi    项目:use   
protected function performAjaxValidation(Model $model)
 {
     $isAjaxRequest = \Yii::$app->request->isAjax();
     $postData = \Yii::$app->request->post();
     if ($isAjaxRequest && $model->load($postData)) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         echo json_encode(ActiveForm::validate($model));
         Yii::$app->end();
     }
 }

作者:yii2tec    项目:admi   
/**
  * @param Model|ActiveRecordInterface $model
  * @return array list of variation models in format: behaviorName => Model[]
  */
 private function findRoleModels($model)
 {
     $roleModels = [];
     foreach ($model->getBehaviors() as $name => $behavior) {
         if (empty($this->roleNames) && $behavior instanceof \yii2tech\ar\role\RoleBehavior || in_array($name, $this->roleNames)) {
             $roleModels[$name] = $behavior->getRoleRelationModel();
         }
     }
     return $roleModels;
 }

作者:chervan    项目:yii2-syn   
/**
  * Init.
  * @throws InvalidConfigException
  */
 public function init()
 {
     parent::init();
     if (!isset($this->id)) {
         throw new InvalidConfigException(get_class($this) . '::$id');
     }
     if (!$this->model instanceof Model) {
         throw new InvalidConfigException(get_class($this) . '::$model');
     }
     $this->model->setScenario('sync');
 }

作者:luyade    项目:luya-cor   
/**
  * Helper method to correctly send model erros and add correct response headers.
  *
  * @param ActiveRecordInterface $model
  * @throws ServerErrorHttpException
  * @return array
  */
 public function sendModelError(Model $model)
 {
     if (!$model->hasErrors()) {
         throw new ServerErrorHttpException('Object error for unknown reason.');
     }
     Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
     $result = [];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result[] = ['field' => $name, 'message' => $message];
     }
     return $result;
 }


问题


面经


文章

微信
公众号

扫码关注公众号