[ThinkPHP] Associated query problems

Source: Internet
Author: User
[ThinkPHP] the ThinkPHP framework is used to develop Association query problems.

The User model is associated with the personalapply table:
'Personalapplication' => array (
'Ing ing _ type' => self: has_counter,
'Foreign _ key' => 'uid ',
'Ing ing _ name' => 'personalapplication ',
),

In the controller: query and filter Fields received by the front-end (the field is the field in the personalapply table)
// Obtain the application information of the Personal Center
$ Limit_year = $ _ GET ['limit _ year'];
$ Loanuse = $ _ GET ['loanuse'];
// Query conditions associated with the application form
$ User = D ('user ');
$ Applymap ['limit _ year'] = $ limit_year;
$ Applymap ['loanuse'] = $ loanuse;
$ Userapply = $ user-> relation ('personalapplication')-> where ($ applymap)-> select (); // only user table fields can be queried, and personalapply table fields cannot be queried,

Question: How can I query the data in personalapply according to the variables after associating the user table with the personalapply table to obtain the associated query data. In the model, condition can only be written to death! Variable cannot be passed. (I have read the manual for a newbie. I don't have it in the manual. I guess I don't know all the functions. if ThinkPHP doesn't work, I will use php SQL to query the functions. please point out the ThinkPHP implementation method, THX !!)


Reply to discussion (solution)

We recommend that you use the View Model for query: write a custom Model (for TP3.2, do not forget to introduce the namespace)

Class UserPersonalapplyViewModel extends ViewModel {public $ viewFields = array ('user' => array ('field 1', 'Field 2', 'Field 3' required for the User table '), 'personalapplication' => array ('personalapply table requires field 1', 'Field 2', 'Field 3', '_ on' => 'User. primary key = personalapply. uid', '_ type' => 'left '),);}


Then, instantiate the model in the controller to retrieve data.
$UserPersonal = D('UserPersonalapplyViewModel');

We recommend that you use the View Model for query: write a custom Model (for TP3.2, do not forget to introduce the namespace)

Class UserPersonalapplyViewModel extends ViewModel {public $ viewFields = array ('user' => array ('field 1', 'Field 2', 'Field 3' required for the User table '), 'personalapplication' => array ('personalapply table requires field 1', 'Field 2', 'Field 3', '_ on' => 'User. primary key = personalapply. uid', '_ type' => 'left '),);}


Then, instantiate the model in the controller to retrieve data.
$UserPersonal = D('UserPersonalapplyViewModel');



We recommend that you use the View Model for query: write a custom Model (for TP3.2, do not forget to introduce the namespace)

Class UserPersonalapplyViewModel extends ViewModel {public $ viewFields = array ('user' => array ('field 1', 'Field 2', 'Field 3' required for the User table '), 'personalapplication' => array ('personalapply table requires field 1', 'Field 2', 'Field 3', '_ on' => 'User. primary key = personalapply. uid', '_ type' => 'left '),);}


Then, instantiate the model in the controller to retrieve data.
$UserPersonal = D('UserPersonalapplyViewModel');



We recommend that you use the View Model for query: write a custom Model (for TP3.2, do not forget to introduce the namespace)

Class UserPersonalapplyViewModel extends ViewModel {public $ viewFields = array ('user' => array ('field 1', 'Field 2', 'Field 3' required for the User table '), 'personalapplication' => array ('personalapply table requires field 1', 'Field 2', 'Field 3', '_ on' => 'User. primary key = personalapply. uid', '_ type' => 'left '),);}


Then, instantiate the model in the controller to retrieve data.
$UserPersonal = D('UserPersonalapplyViewModel');

Slow reply. sorry! Let me give it a try! Thank you!

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.