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

[4.x]: Change in ElementQuery::count effecting EVENT_BEFORE_PREPARE events #15001

Closed
myleshyson opened this issue May 15, 2024 · 4 comments
Closed
Assignees

Comments

@myleshyson
Copy link
Contributor

myleshyson commented May 15, 2024

What happened?

Description

We noticed that after updating to Craft 4.9, one of our queries wasn't running properly. The logic for this particular query depends on the EntryQuery::EVENT_BEFORE_PREPARE event. In particular, in depended on the orderBy property to be null for for count queries to run properly.

Now however, because of this change, the orderBy property is not being nulled like it was before with yiii\db\Query::queryScaler method.

I'm not sure if this was on purpose? Either way would it be possible to either:
a) keep the queryScaler logic from yiii\db\Query::queryScaler for scalar queries (count, min, max, etc) so that things run like they did before the change OR
b) add a property to ElementQuery, something like queryMethod, so that events can add that logic back if the query is a scalar query?

Craft CMS version

Craft 4.9.0

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

@yannkost
Copy link

I have also noticed that the .count() function does not return the right value for some queries.

{% set searchQuery = craft.entries({ type: availableEntryTypes}).search(query).orderBy('score').limit(5) %}

This sample code does return the limit of 5 and not the number of the entries.

@yannkost
Copy link

yannkost commented May 16, 2024

Just checked and without the orderBy('score') the function call returns the correct number.

@i-just
Copy link
Contributor

i-just commented May 17, 2024

@myleshyson, in your case, the EntryQuery::EVENT_BEFORE_PREPARE used to rely on the $event->sender->orderBy returning null, but since 4.9, it’s returning an empty string.

@yannkost, in your case, it’s the combination of search, limit and orderBy('score').

I raised a draft PR, and we’ll discuss this internally.

@brandonkelly
Copy link
Member

Craft 4.9.5 and 5.1.5 are out with a fix that covers both of these issues. Thanks for reporting!

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

No branches or pull requests

4 participants