stibiumz / phalcon.eager-loading

Solves N+1 query problem in Phalcon Model
The Unlicense
36 stars 24 forks source link

Fill $model->_related property on eager load #12

Open Surt opened 7 years ago

Surt commented 7 years ago

Hi, I was taking a look at https://github.com/stibiumz/phalcon.eager-loading/blob/master/src/EagerLoading/EagerLoad.php#L157 the property _related where the relationships are stored is not filled. This is causing me a problem. When I want to check if a relationship is already loaded, (by any way) if not, then I check caché or fetch db.

On pseudocode:

$instances = Loader::from($instances, ['relationshipAlias']);

foreach($instances as $instance) {
    print_r($instance->getRelationshipAlias());
}

// on the model for $instance I have
public function getRelationshipAlias() {
     if(!isset($this->_related['relationshipAlias']) || $this->_related['relationshipAlias'] === null) {
           // get from cache or fetch from db using getRelated('relationshipAlias');
     }
}

The problem here is that on that case, the eagerloaded relationships are wasted. Of course, if I try to check if the relationship is loaded using the property relationshipAlias, in case that the relationship is not loaded it will make a lazy loading "automatically", runining my chance to insert the caché.

Surt commented 7 years ago

The problem here is that _relatedis a protected property :(