The difference between models and ActiveRecord in YII2

Source: Internet
Author: User
Title, want to find out a little YII2 in the model and activerecord the specific role and difference ... Due to the previous use of TP, Inertia felt model used to manipulate the database for CRUD

Reply content:

Title, want to find out a little YII2 in the model and activerecord the specific role and difference ... Due to the previous use of TP, Inertia felt model used to manipulate the database for CRUD

A. Model:
Models is part of MVC and is an object representing 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 inherited directly from Yii\base\model.
Caishuxueqian, I'll explain it in the context of my actual development.
Let me give a practical example, such as the first step before entering the system is the login page, asking the user to enter the user name, password.
After the user enters the user name and password, we verify the user name and password.
Then we need a loginform model, which has two properties: Username,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() {        // 这里写你的验证规则        [['username', 'password'], 'required'],        // password is validated by validatePassword()        ['password', 'checkPassword'], // 验证密码    }    // 这里写你验证密码的逻辑    public function checkPassword($attribute,$params)     {        // ......    }    // 这里写登录的逻辑    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();        // 根据用户在登录表单的输入来做判断        if ($model->load(Yii::$app->request->post()) && $model->login()) {            return $this->goBack();        }    }}

Two. Ar class
Take a look at the text description on the YII Handbook:
The active record (activity log, AR) provides an object-oriented interface to access data in the database. An AR class is associated with a data table, each AR object corresponds to a row in the table, and the properties of the object (that is, the AR attribute attribute) map to the corresponding column of the data row. An Activity record (AR object) corresponds to a row of the data table, and the properties of the AR object map the corresponding column of that row

What exactly is AR class related to model? We look at the code:
Open yii\db\activerecord.php:

class ActiveRecord extends BaseActiveRecord{}

We found that ActiveRecord inherited Baseactiverecord and then opened yii\db\baseactiverecord.php:

abstract class BaseActiveRecord extends Model implements ActiveRecordInterface{}

You can see that the model class is inherited.

Trace the source code, and you'll find the relationship between them.

ActiveRecord inherits from Model to provide data manipulation.

ActiveRecordOne of the methods of database object mapping is to map the database table to the object in the program.
Modelis a generalization of the data model operations.

ActiveRecordCan be understood as a means of manipulating a database (and of course other means, such as direct SQL queries, etc.). ModelIt should be understood that we need to do those things and write them in Model .

Most frameworks like to blend these two things into one object, so there's a bit of confusion in understanding them. In fact ActiveRecord , more use of the object's property parameters (because the database map to store data), and Model more on the object of the method (how to manipulate the data is the process of how to do so).

  • 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.