ThinkPHP model-field and data primary key

Source: Internet
Author: User
Field Definition: The system automatically obtains the field information of the data table when the model is instantiated for the first time (and only once required, the field information will be permanently cached in the future, unless the settings are not cached or deleted ), in debug mode, the field cache file is not generated.

Field definition:The system will automatically obtain the field information of the data table when the model is first instantiated (and only once required, the field information will be permanently cached in the future, unless the settings are not cached or deleted ), if the debugging mode is used, the field cache file is not generated, indicating that the field information of the data table is retrieved again each time.

The field cache is stored in the Runtime/Data/_ fields/Directory. The Cache mechanism is that each model corresponds to a field cache file (not each Data table corresponds to a field cache file). the naming format is:

Database name. model name. php

For example:

Thinkphp. User. php indicates the field cache file generated by the User model.

Thinkphp. Article. php indicates the field cache file generated by the Article model.

You can set the DB_FIELDS_CACHE parameter to disable automatic field caching. if you change the database structure frequently during development, you do not want to cache fields in the data table, you can add the following configuration in the project configuration file:

'Db _ FIELDS_CACHE '=> false

Note:In the debugging mode, field caching is disabled by default because the data structure may change frequently.

To explicitly obtain the field information of the current data table, you can use the getDbFields method of the model class to obtain all the field information of the current data object. for example:

$ Fields = $ User-> getDbFields ();

If you modify the field information of a Data table in the deployment mode, you may need to clear the cache file under the Data/_ fields directory and ask the system to obtain the updated field information of the Data table, otherwise, a new field cannot be written to the database.

If you do not want to depend on the field cache or want to improve performance, you can also manually define the name of the data table field in the model class to avoid IO loading efficiency overhead, add the fields attribute to the model class. The definition format is as follows:

  1. Class UserModel extends Model (){
  2. Protected $ fields = array ('id', 'username', 'Email ',' _ PK' => '', '_ autoinc' => 'true '............)
  3.  
  4. }

_ Pk indicates the primary key field name _ autoinc indicates whether the primary key automatically grows. after the fields attribute is defined, the field information of the data table is not automatically obtained. if the field is modified or added, you must manually modify the value of the fields attribute.

Data primary key

The system automatically identifies the field information and primary key name of the data table in the current operation. the default value is id. the primary key name of the current data object is obtained externally: $ pk = $ Model-> getPk ();

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.