tags:

views:

243

answers:

3

What should happen when I call $user->get_email_address()?

Option 1: Pull the email address from the database on demand

public function get_email_address() {
    if (!$this->email_address) {
     $this->read_from_database('email_address');
    }
    return $this->email_address;
}

Option 2: Pull the email address (and the other User attributes) from the database on object creation

public function __construct(..., $id = 0) {
    if ($id) {
     $this->load_all_data_from_db($id);
    }
}

public function get_email_address() {
    return $this->email_address;
}

My basic question is whether it's best to minimize the number of database queries, or whether it's best to minimize the amount of data that gets transferred from the database.

Another possibility is that it's best to load the attributes that you'll need the most / contain the least data at object creation and everything else on demand.

A follow-up question: What do ORM abstraction frameworks like Activerecord do?

+5  A: 

Minimize the number of queries. The optimal # of queries is 0, but if you must query because it's not cached, it's 1. Querying for every property is a sure fire way to a system that will never scale, has massive contention issues, and will cause way more headaches than its worth.

I should mention that there is value to lazy loading (which is what you're talking about in step 1) if it's unlikely that you will need the data being lazily loaded. If you can though, it's best to be explicit, and fetch exactly or nearly exactly what you need. The less time you spend querying, the less time your connection is open and the more scalable your system is.

aaronjensen
A: 

I would agree with aaronjensen, except when the amount of data you are pulling is to so great that you'll start to use up an excessive amount of memory. I'm thinking where a row has 3 text fields that are all quite large and all you want is the ID field.

Darryl Hein
+7  A: 

There really isn't a correct answer for this. Depends on how many users you're loading at once, how many text/blob fields are in your User table, whether your user table loads any associated child objects. As aaronjensen says, this pattern is called lazy loading - and the opposite behaviour (loading everything up front just in case you need it) is known as eager loading.

That said, there is a third option you might want to consider, which is lazy-loading the entire User object when any of its properties are accessed:

public function get_email_address() {
    if (!$this->email_address) {
        $this->load_all_data_from_db($this->id)
    }
    return $this->email_address;
}

Advantages of this approach are that you can create a collection of users (e.g. a list of all users whose passwords are blank, maybe?) based on their IDs only, without the memory hit of fully loading every single user, but then you only require a single database call for each user to populate the rest of the user fields.

Dylan Beattie
I completely agree, but should add.. Conventionally, this should be done in the DAO (Data Access Object) and not the Model.
Nick Stinemates