[ThinkPHP]关联查询问题

WBOY
Release: 2016-06-23 13:22:13
Original
1272 people have browsed it

使用ThinkPHP框架开发.

User模型里关联了personalapply表:
'personalapply' =>array(
            'mapping_type'=>self::HAS_MANY,
            'foreign_key'=>'uid',
            'mapping_name'=>'personalapply',
        ),

控制器里:根据前端接收的字段查询筛选(字段为personalapply表里字段)
//获取个人中心的申请信息
            $limit_year = $_GET['limit_year'];
            $loanuse = $_GET['loanuse'];
//申请表关联查询条件
            $user=D('user');
            $applymap['limit_year'] = $limit_year;
            $applymap['loanuse'] = $loanuse;
            $userapply=$user->relation('personalapply')->where($applymap)->select();//只能查询user表字段,不能查询personalapply表字段,

问题:想要user表跟personalapply表关联后怎么根据变量查询 personalapply里数据,得出关联后的查询数据。模型里condition只能写死!不能传变量。(我新手也看手册了,手册里没有,估计是我知道的函数不全,ThinkPHP不行我就使用PHP SQL查询了,求大神指出ThinkPHP实现的方法,THX!!)


回复讨论(解决方案)

建议你使用视图模型来查询:写一个自定义Model(如果是TP3.2版本,别忘了引入命名空间)

class UserPersonalapplyViewModel extends ViewModel{	public $viewFields = array(		'User'=>array(			'User表需要取的字段1',			'字段2',			'字段3'		),		'personalapply'=>array(			'personalapply表需要取得字段1',			'字段2',			'字段3',			'_on'=>'User.主键=personalapply.uid',			'_type'=>'LEFT'		),	);} 
Copy after login
Copy after login
Copy after login
Copy after login


然后在控制器里实例化这个模型来取数据即可。
$UserPersonal = D('UserPersonalapplyViewModel');
Copy after login
Copy after login
Copy after login
Copy after login

建议你使用视图模型来查询:写一个自定义Model(如果是TP3.2版本,别忘了引入命名空间)

class UserPersonalapplyViewModel extends ViewModel{	public $viewFields = array(		'User'=>array(			'User表需要取的字段1',			'字段2',			'字段3'		),		'personalapply'=>array(			'personalapply表需要取得字段1',			'字段2',			'字段3',			'_on'=>'User.主键=personalapply.uid',			'_type'=>'LEFT'		),	);} 
Copy after login
Copy after login
Copy after login
Copy after login


然后在控制器里实例化这个模型来取数据即可。
$UserPersonal = D('UserPersonalapplyViewModel');
Copy after login
Copy after login
Copy after login
Copy after login



建议你使用视图模型来查询:写一个自定义Model(如果是TP3.2版本,别忘了引入命名空间)

class UserPersonalapplyViewModel extends ViewModel{	public $viewFields = array(		'User'=>array(			'User表需要取的字段1',			'字段2',			'字段3'		),		'personalapply'=>array(			'personalapply表需要取得字段1',			'字段2',			'字段3',			'_on'=>'User.主键=personalapply.uid',			'_type'=>'LEFT'		),	);} 
Copy after login
Copy after login
Copy after login
Copy after login


然后在控制器里实例化这个模型来取数据即可。
$UserPersonal = D('UserPersonalapplyViewModel');
Copy after login
Copy after login
Copy after login
Copy after login



建议你使用视图模型来查询:写一个自定义Model(如果是TP3.2版本,别忘了引入命名空间)

class UserPersonalapplyViewModel extends ViewModel{	public $viewFields = array(		'User'=>array(			'User表需要取的字段1',			'字段2',			'字段3'		),		'personalapply'=>array(			'personalapply表需要取得字段1',			'字段2',			'字段3',			'_on'=>'User.主键=personalapply.uid',			'_type'=>'LEFT'		),	);} 
Copy after login
Copy after login
Copy after login
Copy after login


然后在控制器里实例化这个模型来取数据即可。
$UserPersonal = D('UserPersonalapplyViewModel');
Copy after login
Copy after login
Copy after login
Copy after login

回复慢了,不好意思!我按你这种方法试一下!谢谢谢谢!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!