Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 ORM Using count before all #6009

Closed
Laykou opened this issue Mar 4, 2015 · 6 comments
Closed

3.0 ORM Using count before all #6009

Laykou opened this issue Mar 4, 2015 · 6 comments
Assignees
Labels
Milestone

Comments

@Laykou
Copy link

Laykou commented Mar 4, 2015

Using count() before all() or any iterator doesn't bind associations:

// Doesn't work
$query = $articles->contain(['Categories'])->hydrate(false);
$total = $query->count();
foreach ($query as $article) {
  $article['category']; // is empty
}
// Works
$query = $articles->contain(['Categories'])->hydrate(false);
foreach ($query as $article) {
  $article['category']; // is ok
}
$total = $query->count();

Was there recently any change in the count() method? The pagination triggers all() before count() therefore it works properly.

@Laykou Laykou changed the title 3.0 Using count before all 3.0 ORM Using count before all Mar 4, 2015
@lorenzo
Copy link
Member

lorenzo commented Mar 4, 2015

@markstory it must be the new autoFields thing

@lorenzo lorenzo added the defect label Mar 4, 2015
@lorenzo lorenzo added this to the 3.0.0 milestone Mar 4, 2015
@markstory
Copy link
Member

It very well could be. @Laykou count() recently changed so that in unbinds all the fields from a query to avoid loading additional fields that are not needed for the count. However, that change was done to a 'clean' copy. Perhaps the copy is still sharing an eagerloader instance.

@markstory markstory self-assigned this Mar 4, 2015
@markusramsak
Copy link
Contributor

I solved this in #6013

@markusramsak
Copy link
Contributor

@Laykou
This problem occurs when you try to execute a count- and then an all-query with the same instance because when executing count then all associated fields are removed therefore they have to be restored.

// this would work because no extra count select would be executed
$query = $articles->contain(['Categories'])->hydrate(false)->all();
$total = $query->count();
foreach ($query as $article) {
  $article['category']; // NOT empty
}

@Laykou
Copy link
Author

Laykou commented Mar 5, 2015

Great, thanks @mdlouhy for the fix, I see what was the problem.
@markstory I discovered that it is how PHP works, it only copies the standard variable types:

When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties.
Any properties that are references to other variables, will remain references.

Closing this as the #6013 is open. @mdlouhy please add tests.

@Laykou Laykou closed this as completed Mar 5, 2015
@Laykou
Copy link
Author

Laykou commented Mar 6, 2015

Just for the record the issue was solved in #6019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants