How do I customize a model? ThinkPHP3.2 the use of custom base class model

Source: Internet
Author: User
This article brings you to the content of how to customize a model? ThinkPHP3.2 the use of custom base class model, there is a certain reference value, the need for friends can refer to, I hope you have some help.

Thinkphp provides a model class for inheriting from other model. The model class is a class of models in MVC, which is the upper class that calls the persistence layer. It's a lot of things to describe, but what can I do? However, this model sometimes does not meet some of our needs, so we need to customize a model class, but the custom model also inherits the model class provided by TP, and our custom model class as the model base class in our project. How do I feel I'm talking tongue twisters and so on ... I'm a little dizzy.

Boring needs.

When using Java's Open source project Jeesite, you develop a bad habit of adding create_by, Create_date, update_by, Update_date, remarks, and del_flag to each table. If each table has these fields, the above fields are set when you insert each table, and some of the fields are updated when you update each table, and the Del_flag field is actually placed when you delete each record. There is a lot of repetition, and some methods of operation are modified. Well, this is the time to customize your own model as the base class for the project, and this model is responsible for doing what I said above.

Customizing a Model

Customize the Add, save, Delete, and select methods in the Model that is at least re-supplied by the TP in a model,model.

The definition code is as follows:

<?phpnamespace Admin\model;use think\model;/** * The base class for other Model classes in the project * This class inherits from the base class provided by TP model */class Basemodel extends Mode L {/** * inherits the base class Model's Add method * Auto Insert ID create_by create_date update_by update_date del_flag */Publ        IC function Add ($data = ", $options =array (), $replace =false) {$data [" id "] = $this->getuuid ();        $data ["create_by"] = "";        $data ["create_date"] = Date ("y-m-d h:i:s");        $data ["update_by"] = "";        $data ["update_date"] = Date ("y-m-d h:i:s");                $data ["del_flag"] = ' 0 ';    Return Parent::add ($data, $options, $replace);        }/** * Get all data not deleted * record is deleted by Del_flag field to determine */Public function Select ($options =array ()) {        $map ["del_flag"] = 0;                $this->where ($MAP);    Return Parent::select ($options); }/** * Gets all the data */Public Function SelectAll ($options =array ()) {return Parent::select ($optio    NS);        }/** * Update Data * Update data, to update update_by update_date two fields */Public Function Save ($data = ", $options =array ()) {        $data ["update_by"] = "";                $data ["update_date"] = Date ("y-m-d h:i:s");    Return Parent::save ($data, $options); }/** * Delete is also update * SET DELETE flag bit */Public Function Delete ($data = ', $options =array ()) {$data ["de                L_flag "] = 1;    Return Parent::save ($data, $options); }

In this way, our basemodel is defined, and later the model of the project will no longer inherit the model class of TP, but inherit our custom Basemodel.

Inherit the base class we define

The inherited Basemodel method is the same as the method that inherits the Model, except that it needs to be modified to our namespace when using use to introduce namespaces to the current scope, instead of using the namespaces provided by TP. The code is as follows:

1 <?php2 namespace Admin\model;3 use Admin\model\basemodel;4 5 class Trunkmodel extends Basemodel {

The namespaces introduced by use are Admin\model\basemodel, because the namespaces we define for Basemodel are Admin\model, because here the Trunkmodel and Basemodel are in a namespace, omitting use is also possible Of

Thus, after we instantiate the Trunkmodel object with the D method, when inserting the data using the Add method, the Add method in Basemodel is called first, so that a series of fields such as each of our tables have create_by are automatically obtained.

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.