CI MSSQL连接和Paginator类的问题

时间:2014-03-12 12:41:29

标签: php sql sql-server codeigniter

我似乎在codeigniter和我的搜索查询中遇到了问题。

代码

public function index($page = 0){
    $this->db->select('a.UserID, a.Username, a.UserFullName, a.UserJoined, 
                      (Select Top 1 p.phoneNumber From vwSelectUserPhone p Where p.userID = a.userID) As PhoneNumber, 
                      (Select Top 1 e1.emailAddress From vwSelectUserEmail e1 Where e1.userID = a.userID) As EmailAddress,
                      (Select Top 1 c1.shipCompany From vwSelectOrderAddresses c1 Where c1.userID = a.userID) As UserCompany');
    //$this->db->order_by('UserJoined', 'DESC')
    $this->db->distinct();
    if($_POST){
        $usid = $this->input->post('UserID');
        $ustxt = $this->input->post('TextSearch');
        if(!empty($ustxt) || !empty($usid)){
            if(!empty($usid)){
                $this->db->where('a.UserID', $usid);
            }elseif(!empty($ustxt)){
                $this->session->set_userdata('USrch_Txt', $ustxt);
                $this->db->join('vwSelectUserAddress b', 'b.userID = a.userID', 'left');
                $this->db->join('vwSelectUserEmail c', 'c.userID = a.userID', 'left');
                $this->db->join('vwSelectUserPhone d', 'd.userID = a.userID', 'left');
                $this->db->join('vwSelectUserNotes e', 'e.userID = a.userID', 'left');
                $this->db->join('vwSelectUserVehicle f', 'f.userID = a.userID', 'left');
                $this->db->like('a.Username', $ustxt);
                $orlike = array('a.UserFullName'=>$ustxt,
                                'b.addressStreet1'=>$ustxt,
                                'b.addressStreet2'=>$ustxt,
                                'b.addressCity'=>$ustxt,
                                'b.addressState'=>$ustxt,
                                'b.addressPostalCode'=>$ustxt,
                                'c.emailAddress'=>$ustxt,
                                'd.phoneNumber'=>$ustxt,
                                'e.Note'=>$ustxt,
                                'f.vehMake'=>$ustxt,
                                'f.vehModel'=>$ustxt,
                                'f.vehYear'=>$ustxt,
                                'f.vehTran'=>$ustxt,
                                'f.vehMake'=>$ustxt,
                                'f.vehEngine'=>$ustxt,
                                'f.vehDescription'=>$ustxt,);
                $this->db->or_like($orlike);
            }
        }
    }
    $this->db->order_by('a.UserFullName', 'asc');
    $this->db->limit(12, $page);
    $qry = $this->db->get('vwSelectUser a');

    $rs = $qry->result_array();
    $config['base_url'] = '/users';
    $config['per_page'] = 12;
    $config['total_rows'] = $this->db->count_all('vwSelectUser');
    $config['num_links'] = 5;
    $config['first_link'] = '<span class="fa fa-angle-double-left page_num"></span>';
    $config['last_link'] = '<span class="fa fa-angle-double-right page_num"></span>';
    $config['cur_tag_open'] = '<span class="page_num bold">';
    $config['cur_tag_close'] = '</span>';
    $config['next_link'] = '<span class="fa fa-angle-right page_num"></span>';
    $config['prev_link'] = '<span class="fa fa-angle-left page_num"></span>';
    $config['uri_segment'] = 2;
    $config['num_tag_open'] = '<span class="page_num">';
    $config['num_tag_close'] = '</span>';
    $this->pagination->initialize($config);
    $data['rs'] = $rs;
    $data['paging'] = $this->pagination->create_links();
    $data['current_page'] = $page;
    $this->load->view('templates/header');
    $this->load->view('pages/users/list', $data);
    $this->load->view('templates/footer');
}

如您所见,我在这里有一些搜索参数。我需要将$config['total_rows']更改为$this->db->count_all_results('vwSelectUser'),但是当我这样做时,我面临以下错误:

Column "vwSelectUser.UserFullName" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

我如何解决这个问题,#1这样它会显示正确的分页链接(如果有的话),#2以获得过滤返回的正确计数?

我通过这样做:$config['total_rows'] = ($_POST) ? $this->db->count_all_results('vwSelectUser a') : $this->db->count_all('vwSelectUser a');它应该做的伎俩,但我得到了指定的错误。

1 个答案:

答案 0 :(得分:1)

我想代码中有很多内容我无法分辨你想要做什么。

1 - 运行SQL事件探查器跟踪以捕获实际发送到数据库引擎的内容。

http://technet.microsoft.com/en-us/library/ms181091.aspx

2 - 所有连接都可以在TSQL语句中完成。实际上,数据库中的用户定义视图可以简化PHP中的调用。

http://technet.microsoft.com/en-us/library/ms181091.aspx

3 - 当您按一组列进行分组并尝试使用不在列表中的列进行排序时,会发生错误代码。

分组时,列必须在列表中,或者在另一列上的聚合函数。

4 - 与任何旧式ASP代码一样,注释掉不起作用的部分。让它工作并逐步添加代码。

简而言之,没有实际的代码和数据库,我只能给你上面的指导方针。我认为1&amp; 2可能会简化代码并让您深入了解错误。

祝你好运,J

PS:注释掉 $ this-&gt; db-&gt; order_by('a.UserFullName','asc'); ,看看计算是否有效。单独调用数据库可以计算总数,但可能不会导致此错误。但是,视图可能肯定会清理代码。