Home > Article > PHP Framework > 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;
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"; }
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!