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:
- Class UserModel extends Model (){
- Protected $ fields = array ('id', 'username', 'Email ',' _ PK' => '', '_ autoinc' => 'true '............)
-
- }
_ 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 ();