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:
1<?PHP2 namespace Admin\model;3 UseThink\model;4 5 /**6 * Base class for other Model classes in the project7 * This class inherits from the base class Model provided by TP8 */9 classBasemodelextendsModel {Ten One /** A * Inherit the base class Model's Add Method - * Auto Insert ID create_by create_date update_by update_date del_flag - */ the Public functionAdd$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"); A $data["del_flag"] = ' 0 '; at - returnParent::add ($data,$options,$replace); - } - - /** - * Get all data not deleted in * Records are deleted through the Del_flag field to determine - */ to Public functionSelect$options=Array()) { + $map["del_flag"] = 0; - $this->where ($map); the * returnParent::select ($options); $ }Panax Notoginseng - /** the * Get all the data + */ A Public functionSelectAll ($options=Array()) { the returnParent::select ($options); + } - $ /** $ * Update Data - * To update update_by update_date two fields when updating data - */ the Public functionSave$data= ",$options=Array()) { - $data["update_by"] = "";Wuyi $data["update_date"] =Date("Y-m-d h:i:s"); the - returnParent::save ($data,$options); Wu } - About /** $ * Delete is also updated - * Set the DELETE flag bit to - */ - Public functionDelete$data= ",$options=Array()) { A $data["Del_flag"] = 1; + the returnParent::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 <? PHP 2 namespace Admin\model; 3 Use Admin\model\basemodel; 4 5 class 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.
Thinkphp 3.2 Custom base class Model