Field definition
Typically, each model class operates on a data table, and in most cases, the system automatically obtains the field information for the current data table.
The system will automatically get the field information of the data table when the model is first instantiated (and only once, the field information will be cached permanently, unless the settings are not cached or deleted), and if the debug mode does not generate a field cache file, it means that the data table field information is retrieved each time.
The field cache is stored under the runtime/data/_fields/directory, and the caching mechanism is a field cache file for each model (note: Not every data table corresponds to a field cache file), and the naming format is:
Database name. Model name (lowercase). php
demo.user.php / / User model generated field cache file // article model generated field cache file
Beginning with the 3.2.3 version, the name of the field cache is adjusted to: database name. data table prefix + model name (lowercase). PHP
demo.think_user.php / / User model generated field cache file // article model generated field cache file
The field cache includes the field information for the data table, the primary key field, and whether it automatically grows, and if field type validation is turned on, including field type information, whether by using the M method or the D method, Or, with native instantiation model classes, the field cache is typically generated whenever the debug mode is not turned on (the field cache can be set off separately).
You can turn off automatic field caching by setting the Db_fields_cache parameter, and if you frequently change the structure of the database at development time rather than the field cache of the data table, you can add the following configuration to the project configuration file:
// close field Cache ' Db_fields_cache ' =false
Note: The default is to turn off the field cache due to the fact that the data structure is subject to frequent changes under debug mode.
If you need to explicitly get field information for the current data table, you can use the Getdbfields method of the model class to get all the field information for the current data object, for example:
$User = M (' User '); $fields $User->getdbfields ();
If you modify the field information of the data table under Deployment mode, you may need to empty the cache file under the Data/_fields directory to get the updated data table field information back, otherwise the new field cannot be written to the database.
If you do not want to rely on field caching or want to improve performance, you can manually define the name of the data table field inside the model class to avoid the efficiency overhead of IO loading, for example:
namespace Home\model; Use Think\model; class extends Model { protected$fieldsarray(' id ', ' username ', ' email ', ' age '); protected$pk = ' id ';}
The PK attribute defines the primary key name of the current data table, and the default is the ID, so there is no need to define it if it is an ID.
3.2.3 versions above, support for defining composite primary keys, for example:
namespace Home\model; Use Think\model; class extends Model { protected$fieldsarray(' user_id ', ' lession_id ', ' score '); protected $PK Array (' user_id ', ' lession_id ');}
In addition to the fields that can be set for the data table, we can also define the type of the field for certain validation links. For example:
namespace Home\model; Use Think\model; class extends Model { protected$fieldsarray(' id ', ' username ', ' email ', ' age ', ' _ Type ' = ' =array(' id ' = ' bigint ', ' username ' = ' varchar ', ' email ' = ' varchar ', ' age ' = ' int ') ) );}
ThinkPHP3.2 Basic Tutorial (6)--Model-field definition