Thinkphp model Design Experience

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to share your experience in designing thinkphp model.
About the model; with the previous article thinkphp directory structure design experience summary http://www.baijunyao.com/article/60 write controller a truth; in order to avoid modification to the framework;
First, we need to have a BaseModel. class. php as our basic model;
I will define the add, delete, and modify methods in BaseModel as follows; Namespace Common \ Model;
Use Think \ Model;
/**
* Basic model
*/
Class BaseModel extends Model {

/**
* Add data
* @ Param array $ data
* @ Return integer the id of the newly added data
*/
Public function addData ($ data ){
$ Id = $ this-> add ($ data );
Return $ id;
}

/**
* Modify data
* @ Param array $ map where statement array form
* @ Param array $ data modified data
* @ Return boolean whether the operation is successful
*/
Public function editData ($ map, $ data ){
$ Result = $ this-> where ($ map)-> save ($ data );
Return $ result;
}

/**
* Delete data
* @ Param array $ map where statement array form
* @ Return boolean whether the operation is successful
*/
Public function deleteData ($ map ){
$ Result = $ this-> where ($ map)-> delete ();
Return $ result;
}


}
Create a model for each table. Place the Model in the/Application/Common/model/directory;
Then all models inherit the BaseModel;
If there are no special requirements for subsequent additions, deletions, and modifications, you can directly call addData, editData, and deleteData;
If you have special requirements, redefine the above methods in the model;
This is equivalent to extending the method in the model without modifying the model of the framework;
The key point is that all additions, deletions, and modifications use the D function instead of the M function to instantiate the model and then call the three methods;
The advantage of doing so is that when the table is changed later, you do not need to call add, save, or delete to change all the places around the world;
Add, delete, modify, and so on;
1: It is strongly recommended that all where conditions use the array format in a uniform way; avoid using the string format where;
2: vertical layout;

Vertical arrangement; obviously better than horizontal sorting; self-comparison is easier to read; high opinion;


3: fixed by field, alias, join, where, order, limit, select;
Why in this order? This is the same as the normal SQL spelling order;
SELECT
U. id,
S .*
FROM
Bjy_student AS s
JOIN bjy_users AS u ON s. uid = u. id
WHERE
S. STATUS = 1
ORDER
Date
LIMIT 10;
4: When you join a table, the first letter of the table is used as the alias. If the first letter of the two tables is the same, the first two letters are used;
This will greatly improve the efficiency of checking and reading the code;

Bai Junyao blog Co., http://www.baijunyao.com/

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.