爱玺玺

爱玺玺的生活日记本。wx:lb87626

thinkphp关联查询

 class IndexModel extends RelationModel{
    protected $_link = array(
            'Users'=>array(//关联表users,必须填写
            'mapping_type'    =>BELONGS_TO,//属于关联
                 'class_name'    =>'Users',//要关联的模型类名,可以不定义,系统会自动找到相关表进行关联
     'foreign_key'=>'pubid',//表index的外键,必须配置
     'mapping_name'=>'users',//和users关联
     'mapping_fields'=>'uname',//关联表users的表uname
             ),
         );
 }


  $Data = D('Index'); // 实例化Data数据对象
  import('ORG.Util.Page');// 导入分页类
  $count      = $Data->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
  $Page       = new Page($count,20);// 实例化分页类 传入总记录数
  $show       = $Page->show();// 分页显示输出
  // 进行分页数据查询
  $list = $Data->relation(true)->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();


模板输出被关联表字段

{#$vo.users.uname#}



貌似只能用被关联表的主键进行关联,关联的时候都没设置这个的地方,看来如果情况特殊还得自己写sql语句。

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5