phproad / phpr-framework

[PHPR Framework] PHPRoad system files
http://phproad.com
MIT License
12 stars 2 forks source link

Db_ActiveRecord should cache mutual/reverse relationships #9

Closed daftspunk closed 8 years ago

daftspunk commented 11 years ago

Take this crude example where:

// 1. select * from users where id = 1
$user = User::create()->find(1);

// 2. select * from user_groups where user_id = 1
$first_group = $user->groups->first();

// 3. select * from users where id = 1 
echo $first_group->user->id;

The SQL statement in 3. is not necessary. When creating the user's collection of groups, each group should have it's spawned/reverse relationship identified and cached up.

daftspunk commented 11 years ago

Note that Db_Data_Collection does in fact cache the parent object.

$user = User::create()->find(1);
$groups = $user->groups;
echo $groups->parent->id;