Like a me, others want the ability the eagerly load models:
- https://github.com/phalcon/cphalcon/issues/1117
- https://forum.phalcon.io/discussion/629/eager-loading
- https://forum.phalcon.io/discussion/3482/model-hierarchy-avoiding-lazy-loading
- https://forum.phalcon.io/discussion/3408/custom-eager-loader
- https://forum.phalcon.io/discussion/933/model-relations-eager-loading-joining-by-relation-alias-names-et
Because this is not implemented yet, I'm trying to write my own eager loader by using the query builder.
In the following code snippet my goal is to fetch all Users with their friends.
$users = $app->modelsManager->createBuilder()
->from('User')
->join('User\UserRelationship', 'User\UserRelationship.user_id_2 = User\User.id')
->getQuery()->execute();
I've defined the relationship in the user class as the following:
$this->hasMany("id", 'user\UserRelationship', "user_id_2", array(
'alias' => 'Friends'
));
Now when I loop the users and fetch their friends it still lazy loads the relationship.
foreach($users as $user){
foreach($user->friends as $friend){
print_r($friend->toArray());
}
}
What am I doing wrong?