thinkphp Model Definition

Source: Internet
Author: User
Tags naming convention

Model classes are generally located under the Lib/model directory of the project, and when we create a Usermodel class, we have actually followed the system's conventions. A naming convention for a model class is a data table name that drops the table prefix, is named with the Hump method, and the first letter is capitalized, and then the model class suffix is defined, for example:
model name (class name) contract corresponding data table (assuming the database prefix definition is THINK_)
Usermodel Think_user
Usertypemodel Think_user_type
If your rule does not match the above system conventions, you need to set the data table Name property of the Model class.
In the thinkphp model, there are several attribute definitions about the name of the data table:
Properties Description
TableName A data table name that does not contain a table prefix, typically the default is the same as the model name, which needs to be defined only if your table name differs from the name of the current model class.
Truetablename The name of the data table that contains the prefix, which is the actual table name in the database, which does not need to be set, only if the above rules do not apply, or in special cases.
DbName Define the database name that the model currently corresponds to, only if you have a different database name and configuration file for your current model class.
Here is an example to deepen understanding, for example, in the database there is a think_categories table, and we define the Model class name is Categorymodel, according to the system, the name of the model is category, the corresponding data table name should be THINK_ Category (all lowercase), but now the data table name is think_categories, so we need to set the TableName property to change the default rule (assuming we have defined db_prefix as think_ in the config file).
    1. protected $tableName = ' categories ';  
Note that the definition of this property does not require a table prefix THINK_
And for another special case, the database has a table (top_depts) prefix and other table prefixes are different, not think_ but top_, this time we need to define the Truetablename property
    1. protected $trueTableName = ' top_depts ';  
Note Truetablename requires a full table name definition
In addition to the definition of a data table, you can define a database, for example:
      Li class= "L0" style= "padding:0px 5px; margin:0px; border-left-width:1px; Border-left-style:solid; Border-left-color:rgb (209,215,220); line-height:23px; Background-color:rgb (250,250,250) "> protected   $dbName   =   ' top '
4

thinkphp Model Definition

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.