stibiumz / phalcon.eager-loading

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

Eager is working but accesing the property re-query it #1

Closed Surt closed 9 years ago

Surt commented 9 years ago

Hi, @stibiumz

with any kind of relationships I can see the eager loading executing. But when I try to access to the property with the alias of the related model it still query the database as always. Using last phalcon 2.0.2


        // on the model I define "sports" relationship
        $this->hasManyToMany(
            "id",
            'App\Sport\Models\SportUser',
            "user_id", "sport_id",
            'App\Sport\Models\Sport',
            "id", array('alias' => 'sports')
        );

        // after... on a controller I use it this way:

        $user = User::findFirst(1)->load('sports');
        var_dump($user->sports);
SELECT `user`.`id`, `user`.`name`, `user`.`email`, `user`.`telephone`, `user`.`password`, `user`.`avatar`, `user`.`gender`, `user`.`birth`, `user`.`role`, `user`.`active`, `user`.`date_created`, `user`.`date_updated`, `user`.`deleted` FROM `user` WHERE `user`.`id` = 1 LIMIT 1
SELECT `sport`.`id`, `sport`.`name` FROM `sport`  INNER JOIN `sport_user` ON `sport_user`.`sport_id` = `sport`.`id` WHERE `sport_user`.`user_id` IN (:phi0)

// executed on the var_dump
SELECT `sport`.`id`, `sport`.`name` FROM `sport`  INNER JOIN `sport_user` ON `sport_user`.`sport_id` = `sport`.`id` WHERE `sport_user`.`user_id` = :0
Surt commented 9 years ago

It seems like a problem with the properties assignment.

Time: 293 ms, Memory: 15.00Mb

There were 5 failures:

1) EagerLoadingTests::testBelongsTo
Failed asserting that false is true.

/var/www/site/vendor/stibium/phalcon.eager-loading/tests/EagerLoadingTests.php:22

2) EagerLoadingTests::testBelongsToDeep
Failed asserting that false is true.

/var/www/site/vendor/stibium/phalcon.eager-loading/tests/EagerLoadingTests.php:60

3) EagerLoadingTests::testHasMany
Failed asserting that false is true.

/var/www/site/vendor/stibium/phalcon.eager-loading/tests/EagerLoadingTests.php:88

4) EagerLoadingTests::testHasManyToMany
Failed asserting that false is true.

/var/www/site/vendor/stibium/phalcon.eager-loading/tests/EagerLoadingTests.php:115

5) EagerLoadingTests::testManyEagerLoadsAndConstraints
Failed asserting that '8' matches expected 25.

/var/www/site/vendor/stibium/phalcon.eager-loading/tests/EagerLoadingTests.php:217

FAILURES!                              
Tests: 19, Assertions: 26, Failures: 5.
stibiumz commented 9 years ago

Hi, thanks for reporting

Are you using Phalcon 2.x, right?

Surt commented 9 years ago

@stibiumz yes, 2.0.2

Surt commented 9 years ago

Last time I checked it the problem with this was related to the ResultSets Simple using the pdo resource to traverse the results, not taking in account the "rows" already fetched, so any assignment done to it, when traversing is lost.

stibiumz commented 9 years ago

The problem is probably here, the property is not set. I'll try to fix it this afternoon and make sure that works well with Phalcon 2

stibiumz commented 9 years ago

It's working now, I've tested it with Phalcon 2.0.2 and in 1.3.4

Surt commented 9 years ago

Working as a charm! really need to move it to the incubator so many ppl can use it and the phalcon team could merge it to the framework.

stibiumz commented 9 years ago

Today I'll send a pull request to the incubator repo :thumbsup: