D () method search order, Method Search Order
The D () method is more intelligent than the model class. If the model class cannot be found in HomeModelUserModel, CommonModelUserModel will be searched in the public module. If not, the base class Model () class is directly instantiated, which is equivalent to the M () method.
Note that the table name in the D ('user') method must start with an uppercase letter, because the lower-case User cannot be identified when the query jumps to the public module, unless you explicitly write: D ('common/user'), which can be written in lowercase. Otherwise, if you use D ('user') directly, it will skip the public module and directly instantiate the base class Model (). Therefore, we recommend that you create the first letter of a table to avoid errors.
Of course, the above problem is mainly caused by the use of lower-case user:
The D ('user') method can be identified in the Home module, and it is no problem to directly instantiate it. It only happens when cross-module (such as Common) questions are imitated. Of course, the M ('user') method can also be identified in lower case.
In addition, if you use an index array as the query condition, note the following:
If the model found by the D ('user') method has manually defined data fields, the query will not take effect. This means:
Namespace HomeModel;
Use ThinkModel;
Class UserModel extends Model {
Protected $ fields = array (// This is a manually defined field
'Id ',
'User ',
'_ PK' => 'id ',
'Type' => array (
'Id' => 'smallint ',
'User' => 'varchar'
)
);
//...
}
When the following code is used in the controller:
$ User = D ('user ');
$ C ['id'] = 2;
$ C ['user'] = 'cherry meatballs ';
Print_r ($ user-> where ($ c)-> select (); // use the index array for query
No results will be found unless the manual field is commented out or the M ('user') method is used.