CodeIgniter distinct功能不起作用

时间:2013-03-19 13:19:59

标签: php mysql codeigniter distinct

这是我想要做的。 我希望我的左手菜单是基于群组的。如果我已向群组授予查看菜单的权限,那么该群组应该可以看到它。

现在开始进一步询问细节。 我想向您展示我到目前为止所做的工作以及我应用此特定菜单组权限的位置。

以下是核心文件夹中名为My_Controller.php的基本控制器。

我的Main控制器已从My_Controller扩展。

以下是My_Controller编码。

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: SBPS
 * Date: 3/13/13
 * Time: 4:55 PM
 * To change this template use File | Settings | File Templates.
 */

class My_Controller extends CI_Controller{
    function __construct(){
        parent::__construct();
    }
    public function UserGroups(){
        $UserID=1;
        $this->load->model('ui_components/Left_menu_cpanel');
        $Groups=$this->Left_menu_cpanel->get_user_groups($UserID);
        return $Groups;
    }
}

这是我的Main控制器。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends My_Controller {
    /*
     * Main Controller
     * Designed By: Kifayat Ullah
     * Purpose: To work as a main Control Panel for the Authenticated users
    */


    public function index() {
        if(!$this->session->userdata('UserID')
         || $this->session->userdata('UserID')<=0){
             redirect('user_management/login_form');
         }// end of session check
        // Prepare the LeftHandSide Menu Object and Send it to the View for Display
        $this->load->model('ui_components/Left_menu_cpanel');
        $UserGroups=$this->UserGroups();

         // retrive menu List object


        // Pass the name of the view that you want to load
        $data['main_container']='tamplet_includes/tamplet_main_container_tags'; 
        $data['UserGroups']=$UserGroups;
        //$data['UserGroups']=$this->UserGroups();
        $this->load->view('SystemView',$data);

        //var_dump($UserGroups);
    }

    function UserAdditionForm(){

        echo 'hello';
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

现在是模特的转折。

我的基本型号名称为核心文件夹中的My_Model.php。 我的left_menu_cpanel已从My_Model.php延长。

My_Model.php基本模型文件:

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: SBPS
 * Date: 3/19/13
 * Time: 3:13 PM
 * To change this template use File | Settings | File Templates.
 */

class My_Model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    public function get_user_groups($UserID){
        //$UserID=1; // We will Update this UserID Later
        $this->load->helper('security');
        $this->db->select('*');
        $this->db->from('sys_user_accounts');
        $this->db->join('sys_user_groups_memberships', 'sys_user_groups_memberships.UserID = sys_user_accounts.UserID', 'INNER');
        $this->db->where('sys_user_accounts.UserID', $UserID);
        $this->db->join('sys_user_groups', 'sys_user_groups.GroupID = sys_user_groups_memberships.GroupID', 'INNER');
        $query = $this->db->get();

        return $query->result(); // return all forms list

    }
}

left_menu_cpanel.php模型文件:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Left_menu_cpanel extends My_Model {
  function __construct()
    {
        parent::__construct();
    }

    // This function returns true if a user authenticaton is successfull
    public function get_left_menu_items($GroupID){
        //$GroupID=1; // We need update this to a Group Array from the users permissions list
        $this->load->helper('security');
        $this->db->distinct();
        $this->db->select('*');
        $this->db->from('sys_group_roles_forms_view');
        $this->db->where('GroupID', $GroupID);
        $query = $this->db->get();  

        return $query->result(); // return all forms list       

    }// end of authenticate function
}//end of class
/* End of file left_menu_cpanel.php.php */
/* Location: ./application/models/user_management/left_menu_cpanel.php.php */
?>

我的表sys_group_roles_forms_view的结构 enter image description here

我的sys_user_groups enter image description here

用户可以属于多个群组,群组可以拥有多个用户。

现在我认为我已经发布了所有细节,

所以我的问题是,如果我已经允许分组查看某个标签,那么如果我获得了两个或更多组的权限,那么只有标签应该出现在一次。

但是由于用户属于3个不同的群体,所以因为这个原因它重复Tab 3次。但我只想要一次。

按Tab我的意思是左侧菜单重复三次。 enter image description here

我试图通过网络搜索解决方案,我得到了具有独特功能的解决方案。但是这个功能并没有像我一样努力。

请让我理解为什么该功能不起作用,如果它在我的场景中有效,那么如何应用该功能?

1 个答案:

答案 0 :(得分:2)

Distinct对整行进行操作,包括组ID,组名等。您只需选择所需的列(避免select *)并使用group_by()方法。< / p>