本文整理汇总了PHP中yii\base\Model类的典型用法代码示例。如果您正苦于以下问题:PHP Model类的具体用法?PHP Model怎么用?PHP Model使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Model类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @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);
}
示例2: performValidationModel
/**
* 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;
}
示例3: performAjaxValidation
/**
* 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();
}
}
示例4: onLoad
/**
* Берет значения из 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;
}
示例5: validateAttribute
/**
* 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);
}
}
示例6: modelErrors
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);
}
}
示例7: load
/**
* 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;
}
示例8: getHotlinkTo
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);
}
示例9: getIndexGridColumns
/**
* 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));
}
示例10: serializeModelErrors
/**
* 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;
}
示例11: checkRequiredParams
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;
}
示例12: set
/**
* @inheritdoc
*/
public function set(Model $model)
{
if (Yii::$app->user->getIsGuest()) {
return;
}
$data = $model->toArray();
$this->getStorage()->setBounded('theme', $data);
$this->setToCache($data);
}
示例13: prepareMessages
/**
* 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;
}
示例14: errorsToString
/**
* 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;
}
示例15: getModelDirectory
/**
* @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);
}