D () method search order, Method Search Order

Source: Internet
Author: User

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.

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.