CodeIgniter Forums

Full Version: Query Builder Select method using array as fieldnames
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The documentation mentions you can select fields like this:


PHP Code:
$this->db->select('user_id, user_name, user_email'); 


What it doesn't mention is that this also seems to work:


PHP Code:
$this->db->select( array('user_id''user_name''user_email') ); 


This is actually really useful when I have a model method which allows you to select whatever fields you require at the time, i.e:


PHP Code:
public function get_user($user_id$fields = array('*')) {
    $query $this->db->select$fields )
                  ->
from('user')
                  ->
where( array('user_id' => $user_id) )
                  ->
get();

    return $query->row_array();
}

$user $this->user_model->get_user(1); // Get user with all fields
$user $this->user_model->get_user(1, array('user_email')); // Get just the user's email address 


My question is, is being able to use $this->db->select() in this way intentional, or just a lucky mistake?
Intentional.
(06-23-2017, 04:17 AM)Narf Wrote: [ -> ]Intentional.

That's great to know, thank you!