A summary of the design experience of thinkphp model models

Source: Internet
Author: User

about the model; to keep up with the previous article thinkphp's directory structure design experience summarizes write controller a principle, in order to avoid changes to the framework;

First of all we are going to have a BaseModel.class.php as our basic model;

I will define additions and deletions in the Basemodel the following methods;

<?php
namespace Common\model;
Use Think\model;
/**
* Basic Model
*/
Class Basemodel extends model{

/**
* Add data
* @param array $data data
* @return The ID of the integer new data
*/
Public Function AddData ($data) {
$id = $this->add ($data);
return $id;
}

/**
* Modify Data
* @param array Form $map the WHERE statement
* @param array $data modified data
* @return Whether the Boolean operation was successful
*/
Public Function EditData ($map, $data) {
$result = $this->where ($map)->save ($data);
return $result;
}

/**
* Delete Data
* @param array Form $map the WHERE statement
* @return Whether the Boolean operation was successful
*/
Public Function DeleteData ($map) {
$result = $this->where ($map)->delete ();
return $result;
}


}

Build a model for each table; Unified in the/application/common/model/directory;

Then all the models inherit Basemodel;

Later additions and deletions if there is no special needs, you can directly call AddData, EditData, DeleteData;

If there are special needs, redefine the above methods in the model;

The practical meaning is to extend the method of model without changing the model of the frame;

The key point is to say that all the additions and deletions are changed; use the D function instead of the M function to instantiate the model and invoke these 3 methods;

The advantage of this is that when the table is changed later, it is not necessary to go to all the world to find the places called Add, save, delete one by one to change;

Additions and deletions have been said;

1: It is strongly recommended that all where conditions be uniformly used in array format; avoid the where in string format;

2: Vertical typesetting;

The vertical arrangement is better than the horizontal one; it is easier to read by comparison.

3: Fixed in accordance with field, alias, join, where, order, limit, select;

Why do you follow this order? Because this is consistent with the order in which we normally spell SQL;

SELECT
U.id,
s.*
From
Bjy_student as S
JOIN bjy_users as u on s.uid = U.id
WHERE
S. STATUS = 1
ORDER by
Date
LIMIT 10;

The 4:join of the table; the first letter of the table, if the first letter of the two tables is the same, then the top two letters;

After this specification, it will greatly improve the efficiency of checking reading code;

This article for Bai Jun Remote original article, reprinted without contact with me, but please specify from Bai Jun remote blog http://www.baijunyao.com

A summary of the design experience of thinkphp model models

Related Article

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.