Welcome Guest, Not a member yet? Register   Sign In
reduce multiple qrys?
#1

[eluser]taschentuch[/eluser]
hi i have a question.

i write a permission function for a ACL lib:

my code:

model:

Code:
function get_userperm_by_uid($user_id)
    {
        $this->db->where('user_id', $user_id);
        $query = $this->db->get('permission');
        return $query->row_array();
    }
lib:

Code:
function get_perm()
    {
        $user_id = $this->ci->session->userdata('user_id');
        return $this->ci->users->get_userperm_by_uid($user_id);

    }

helper:


Code:
function check($perm)
{
    $CI = & get_instance();

    if ($CI->tank_auth->is_logged_in()) {

    $user_perms = $CI->tank_auth->get_perm();


            if($user_perms["$perm"] == "yes") {
                return TRUE;
    
            } else {
                return FALSE;    
            }

    } else {
        return FALSE;
    }

}

using:

Code:
if(check("write_news"))
{
    //do something
}

but for each check() is one mysqlqry an im need to use this function about 30-40 times
Quote:0.0013 SELECT *
FROM (`permission`)
WHERE `user_id` = '1'
0.0014 SELECT *
FROM (`permission`)
WHERE `user_id` = '1'
0.0014 SELECT *
FROM (`permission`)
WHERE `user_id` = '1'

is there a way to reduce this to 1 qry? it should only 1 qry for all checks

thx
#2

[eluser]mattpointblank[/eluser]
Do the permission check at the start of the code and store it in flashdata or something?
#3

[eluser]rogierb[/eluser]
The first time you do a check inn you helper function, load all the permissions for a user in an array.
The second time, check the array and get the permission from the array.
#4

[eluser]taschentuch[/eluser]
okay i change my code so:

on userlogin i get the permissions and save the fields which are true with implode in the session
on check i split the sessiondata into an array and check with array_key_exists

is this better so? and possible?



thx =)




Theme © iAndrew 2016 - Forum software by © MyBB