Sizzling Code
Sizzling Code

Reputation: 6070

CodeIgniter distinct function is not working

Here is what i am trying to do. I want my Left Menus to be Group Based. If i have given the Permission to the Group to view the Menu then it should be visible to that group.

Now Before Going to further in question details. I want to show you what i have done till far and where i am applying this specific menus group Permissions.

Here is Base Controller Named My_Controller.php in Core Folder.

My Main Controller extended from My_Controller.

Here is My_Controller Coding.

<?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;
    }
}

Here is my Main Controller.

<?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 */

Now its The Turn Of Models.

My Base Model Named is My_Model.php in Core Folder. My left_menu_cpanel is extended from My_Model.php.

My_Model.php Base Model file:

<?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 Model file:

<?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 */
?>

My table Structure of sys_group_roles_forms_view enter image description here

My sys_user_groups enter image description here

User Can Belong to Many Groups, and a group can have Many Users.

Now as i think i have posted every details,

So my Question is that if i have given a permission to group to view certain Tab, so if i that permission is given to two or more groups then only the tab should appear One Time.

But as User Belongs to 3 Different Groups so cuz of this reason it is repeating the Tab 3 times. But i want it only One Time.

By Tab i mean Left Side Menus Repeating Three Times. enter image description here

i tried to search over the Net for solution, and i got the solution for it of distinct function. But that function is not working as i tried.

Please Let me Understand why that function is not working, and if it will work in my scenario then how to apply that function??

Upvotes: 1

Views: 1518

Answers (1)

mcrumley
mcrumley

Reputation: 5700

Distinct operates on the whole row, including the group id, group name, etc. You need to select only the columns that you need (avoid select *) and use the group_by() method instead.

Upvotes: 2

Related Questions