Welcome Guest, Not a member yet? Register   Sign In
Passing too much information?
#1

[eluser]theshiftexchange[/eluser]
Hi guys,

I've got a theoretical question: should I try to reduce the amount of data in my variables been passed to my view?

To explain: I have a db with lots of tables. One table is CustomerProfile. This table has approximately 12 columns of generic data about the customer (nothing secret to the customer who is logged in). Note: the user password etc is not in this table.

Now some of my views only need 2-3 columns of this data to display what I want. My question is; should I just get the whole row of the customerprofile, and pass that to my view, and just use the variables I want? Or should I write a new function in my model, to select only the columns I need, and pass that back?

I'm guessing passing everything causes more memory use on your server, while passing just the columns will cause more sql processing (cpu) (and more programming time since I need to adjust the model)?

Cheers
#2

[eluser]xwero[/eluser]
A quick solution to use the fastest queries and the least data using AR
Code:
function get_profiles($fields)
{
    if(!empty($fields){ $this->db->select($fields); }

    return $this->db->get('table')->result();
}

function get_profile($id,$fields='')
{
   if(!empty($fields){ $this->db->select($fields); }

   return $this->db->get_where('table',array('id'=>$id))->row();
}
It's too bad the get and get_where method don't provide this option themselves because i think there are more queries where you need to get specific fields/columns than there are getting all the fields from a table.
#3

[eluser]Dam1an[/eluser]
In all the get functions in my models, I have an optional select parameter which defaults to everything
Code:
// The function
function get_users($select = '*') {
    $this->db->select($select);
    $query = $this->db->get('users');
    
    return $query->num_rows() == 0 ? false : $query->result();
}

// Getting all the fields
$users = $this->user_model->get_users();

// get just their name and surname
$users = $this->user_model->get_users('first_name, last_name');




Theme © iAndrew 2016 - Forum software by © MyBB