For example, I want to figure out the specific functions and differences between model and activeRecord in YII2... due to the previous use of TP, I felt that the model was used to operate the database for CRUD. I wanted to find out the specific roles and differences between the model and activeRecord in YII2... since we used TP, we feel that the model is used to operate databases for CRUD.
Reply content:
For example, I want to figure out the specific functions and differences between the model and activeRecord in YII2... since I used TP, I feel that the model is used to operate databases for CRUD.
I. Model:
Models is a part of mvc and an object that represents business data, rules, and logic.
The Model class is also the base class for more advanced models such as Active Record activity records
By default, the Model is directly inherited from yii \ base \ Model.
I will explain the scenarios I encountered in actual development.
Let me give you a practical example. For example, the first step before entering the system is to log on to the page and ask the user to enter the user name and password.
After entering the user name and password, we will verify the user name and password.
At this time, we need a LoginForm model, which has two attributes: username and password.
Let's look at the model code:
Namespace app \ models; use yii \ base \ Model; class LoginForm extends Model {public $ username; public $ password; public function rules () {// enter your verification rules [['username', 'Password'], 'required'], // password is validated by validatePassword () ['Password ', 'checkpassword'], // verify the password} // write the public function checkPassword ($ attribute, $ params ){//......} // write the logon logic public function login (){//......}}
Let's look at the controller code:
Namespace app \ controllers; use Yii; use yii \ web \ Controller; class SiteController extends Controller {//... public function actionLogin () {$ model = new LoginForm (); // judge if ($ model-> load (Yii :: $ app-> request-> post () & $ model-> login () {return $ this-> goBack ();}}}
Ii. AR class
Let's take a look at the text description in the yii manual:
Active Record (AR) provides an object-oriented interface to access data in the database. An AR class associates a data table. Each AR object corresponds to a row in the table. The attributes of the object (that is, the Attribute of the AR) are mapped to the corresponding columns of the Data row. An active record (AR object) corresponds to a row of the data table, and the attributes of the AR object map to the corresponding columns of the row.
What is the relationship between the AR class and the model? From the Code:
Open yii \ db \ ActiveRecord. php:
class ActiveRecord extends BaseActiveRecord{}
We found that ActiveRecord inherits BaseActiveRecord, and then open yii \ db \ BaseActiveRecord. php:
abstract class BaseActiveRecord extends Model implements ActiveRecordInterface{}
We can see that the Model class is inherited.
Trace the source code and discover the relationship between them.
ActiveRecord inherits from Model to provide data operations.
ActiveRecord
One method of database object ing is to map database tables to objects in programs.
Model
This is a summary of Data Model operations.
ActiveRecord
It can be understood as a means of operating the database (of course there are other means, such as directSQL
Query ). WhileModel
It should be understood that we need to perform those operations and write them inModel
.
Most frameworks like to combine these two things into an object, so there will be some confusion in understanding. ActuallyActiveRecord
The object's attribute parameters are used more (because the data mapped to the database is stored ).Model
The object method (the operation method is the process of operating data ).