taylorotwell/eloquent — Issue #5: Auto (eager)load a relation for a Model
Author: Vespakoen
Created: 2012-01-30 12:51:48+00:00
State: open

Hey Taylor,

I have been thinking about a way to be able to "auto (eager)load" a relation.

My first guess is that it should look something like this:

class Role extends Eloquent/Model {

public $include = array('Role_lang' => array('language_id', '=', Auth::user()->language_id));

function role_lang()
{
$this->has_many('Role_lang');
}
}

Now if i would do:
Role::all();

I should produce something like:

array
1 =>
object(Role)[49]
public 'include' =>
array
'Role_lang' =>
array
0 => string 'language_id' (length=11)
1 => string '=' (length=1)
2 => int 1
public 'query' => null
public 'exists' => boolean true
public 'attributes' =>
array
'id' => int 1
'key' => string 'superadmin' (length=10)
public 'role_lang' => object(Role_lang)[12]
public 'attributes' =>
array
'language_id' => 1,
'name' => 'Super admin',
'description' => 'This is the super admin in english',
public 'dirty' =>
array
empty
public 'ignore' =>
array
empty
public 'includes' =>
array
empty
public 'relating' => null
public 'relating_key' => null
public 'relating_table' => null
2 =>
object(Role)[43]
...

This is just an idea, and I hope it makes some sense.
If we can agree on how this should be implemented, I am willing to contribute my part.

Greetz!