Thinkphp Model Naming conventions

Source: Internet
Author: User
Tags naming convention

a little problem for a long time: if the database is underlined in the table name, then in the automatic completion with thinkphp Note that the name of the model class to become the hump method, the file name and class names to change. (Note also: You can call to auto-Validate and auto-complete only when creating data using the Create method)thinkphp Model Naming conventionsModel Naming conventions
thinkphp the name of the database and the model class are subject to certain specifications. First, the table names and fields of the database are all lowercase, and the naming convention of the model class is the name of the data table that drops the table prefix, with the first letter capitalized, followed by the suffix definition of the model class. Some examples are as follows:
Table name (without prefix) model name (class name) FileName Example
UserUserModelUserModel.class.phpclass Usermodel extends model{}
User_groupUserGroupModelUserGroupModel.class.phpclass Usergroupmodel extends model{}
Tip: The system identifies the underlined table name with the Hump naming method.
When the table naming rules in the project do not match the conventions in thinkphp (which is often the case), thinkphp allows the TableName and Truetablename properties of the Model class to be set for compatibility purposes.
TableName Property
When the table prefix and system setting prefix (DB_PREFIX) are consistent and the table name and model name are inconsistent, you can set this property, for example, if the table name is users and the model name is Usermodel, then you need to set the model class:
Class Usermodel extends model{protected $tableName = ' users ';}
Truetablename Property
If the table prefix is inconsistent with the System settings (table name and model name may be the same), you need to set the Truetablename property of the model:
Class Usermodel extends model{protected $trueTableName = ' My_user ';}
Note The Truetablename value is the full table name (including the prefix).
DbName Property
The DbName property defines the database name that the model currently corresponds to, only if the database name and configuration file corresponding to the current model class are different:
protected $dbName = ' cdb ';
Table suffix
When the table suffix is set, the system automatically recognizes the table suffix, and the corresponding definition is as follows:
Table name (without prefix) suffix model name (class name) filename
log001001 LogModelLogModel.class.php
Of course, if you do not define a suffix, you can also define the TableName property individually for each model class, depending on the actual strategy.

Thinkphp model Naming conventions

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.