Home > PHP Framework > YII > What is the yii framework model

What is the yii framework model

(*-*)浩
Release: 2019-11-30 15:17:50
Original
2692 people have browsed it

What is the yii framework model

The yii framework model is part of the MVC pattern and is an object that represents business data, rules and logic.

You can define model classes by inheriting yii\base\Model or its subclasses. The base class yii\base\Model supports many practical features:

Attribute: Represents business data that can be accessed like ordinary class attributes or arrays; (Recommended learning: yii framework)

Attribute label: The label displayed by the specified attribute;

Block assignment: supports assigning values ​​to many attributes in one step;

Validation rules: ensures that the input data conforms to the declared validation rules;

Data export: allows model data to be exported to a custom format array.

The Model class is also the base class for more advanced models such as Active Record.

Information: Models are not required to inherit yii\base\Model, but since many components support yii\base\Model, it is best to use it as the model base class.

Attributes

The model represents business data through attributes. Each attribute is like a publicly accessible attribute of the model, specified by yii\base\Model::attributes() Properties owned by the model.

You can access the properties of a model like an object property:

$model = new \app\models\ContactForm;
// "name" 是ContactForm模型的属性
$model->name = 'example';
echo $model->name;
Copy after login

You can also access the properties like an array cell item, thanks to yii\ base\Model supports ArrayAccess array access and ArrayIterator array iterator:

$model = new \app\models\ContactForm;
// 像访问数组单元项一样访问属性
$model['name'] = 'example';
echo $model['name'];
// 迭代器遍历模型
foreach ($model as $name => $value) {
    echo "$name: $value\n";
}
Copy after login

The above is the detailed content of What is the yii framework model. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template