cakephp2.X多表联合查询join及使用分页查询的方法

时间:2022-02-21 00:34:39

本文实例讲述了cakephp2.X多表联合查询join及使用分页查询的方法。分享给大家供大家参考,具体如下:

格式化参数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public function getconditions($data){
  $this->loadModel("Cm.LoginHistory");
  $conditions = array();
  foreach ($data as $key=>$val){
    if($key=='start_date'){
      $conditions['LoginHistory.logintime >=']=trim($val);
    }elseif ($key=='end_date'){
      $conditions['LoginHistory.logouttime <=']=trim($val);
    }elseif ($key=='selectvsoftid' and $val !=''){
      $conditions['LoginHistory.LOGINSUBSYSTEM LIKE']='%'.trim($val).'%';
    }elseif ($key=='username' and $val !=''){
      $conditions['LoginHistory.USERNAME LIKE']='%'.trim($val).'%';
    }elseif ($key=='vdevicename' and $val !=''){
      $conditions['LoginHistory.WINDOWNAME LIKE']='%'.trim($val).'%';
    }elseif ($key=='selectvsoftid' and $val !=''){
      $conditions['LoginHistory.PHDEVICENAME LIKE']='%'.trim($val).'%';
    }
  }
  return $conditions;
}

分页查询:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$fields = array('LoginHistory.windowname','LoginHistory.loginsubsystem','VirtualDevice.PHDEVICEID','LoginHistory.userid', 'VirtualDevice.SYSTYPEID','UserInfo.USERNAME','"SUM"(LoginHistory.LOGOUTTIME-LoginHistory.LOGINTIME) moument','UserInfo.USERNAME','DeviceInfo.DEVICENAME');
$group=array('LoginHistory.windowname', 'LoginHistory.loginsubsystem', 'LoginHistory.userid','UserInfo.USERNAME','DeviceInfo.DEVICENAME','VirtualDevice.SYSTYPEID','VirtualDevice.PHDEVICEID');
$this->paginate = array('conditions'=>$conditions,'fields'=>$fields,'group'=>$group,'limit'=>9,'joins' => array(
   array(
     'alias' => 'UserInfo',
     'table' => 'SMM_USERINFO',
     'type' => 'LEFT',
     'conditions' => ' LoginHistory.USERID=UserInfo.USERID ',
   ),
   array(
     'alias' => 'VirtualDevice',
     'table' => 'ET_NMVIRTUALDEVICEINFO',
     'type' => 'LEFT',
     'conditions' => ' LoginHistory.WINDOWNAME=VirtualDevice.DEVICENAME ',
   ),
   array(
     'alias' => 'DeviceInfo',
     'table' => 'ET_NMDEVICEINFO',
     'type' => 'LEFT',
     'conditions' => ' VirtualDevice.PHDEVICEID=DeviceInfo.DEVICEID ',
   ),
 ));
$LoginHistory = $this->paginate('LoginHistory');
$this->set('loginhistory',$LoginHistory);

希望本文所述对大家PHP程序设计有所帮助。