[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!