Differences between models and activeRecord in YII2

Source: Internet
Author: User
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.

ActiveRecordOne method of database object ing is to map database tables to objects in programs.
ModelThis is a summary of Data Model operations.

ActiveRecordIt can be understood as a means of operating the database (of course there are other means, such as directSQLQuery ). WhileModelIt 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. ActuallyActiveRecordThe object's attribute parameters are used more (because the data mapped to the database is stored ).ModelThe object method (the operation method is the process of operating data ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.