Passing arguments and conditions to model in codeigniter

Posted by stormdrain on Stack Overflow See other posts from Stack Overflow or by stormdrain
Published on 2010-05-06T16:03:26Z Indexed on 2010/05/06 16:08 UTC
Read the original article Hit count: 192

Filed under:
|

I'm adding some models to a project, and was wondering if there is a "best practice" kind of approach to creating models:

Does it make sense to create a function for each specific query?

I was starting to do this, then had the idea of creating a generic function that I could pass parameters to. e.g:

Instead of

function getClients(){
    return $this->db->query('SELECT client_id,last FROM Names ORDER BY id DESC');
    }
    function getClientNames($clid){
        return $this->db->query('SELECT * FROM Names WHERE client_id = '.$clid);
    }
    function getClientName($nameID){
        return $this->db->query('SELECT * FROM Names WHERE id ='.$nameID);
    }
}

Something like

function getNameData($args,$cond){
    if($cond==''){
        $q=$this->db->query('SELECT '.$args.' FROM Names');
        return $q;
    }else{
        $q=$this->db->query('SELECT '.$args.' FROM Names WHERE '.$cond);
        return $q;
    }
}

where I can pass the fields and conditions (if applicable) to the model. Is there a reason the latter example would be a bad idea?

Thanks!

© Stack Overflow or respective owner

Related posts about codeigniter

Related posts about models