-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Having in query builder breaks total pages and total items of paginator #12111
Comments
@sergeyklay @Jurigag Can you check it, please? Maybe I am doing something wrong... |
Count is done BEFORE having clause. And it's not phalcon issue, it's just in all SQL based databases. It's more like an NFR/Enhancment than a bug imho. |
And IMHO it's a bug because SQL response is different than paginator expected result. You can use neither aliases nor SUM/COUNT... functions in WHERE so HAVING is the only way here and it must be working properly |
What ? But you didnt do COUNT bro. You just did select. Check COUNT(*) on this query. It will return 3 :). It's not a bug as it is, HAVING is done AFTER COUNT in SQL. But sure something has to be done to make it working properly and as it should be. |
Oh, right, you are right. I didn't get whant you mean. Neither did I know about that count problem in SQLs, sorry. But still, paginator is working differently than expected so it should be somehow improved. Do you have an idea how can we do it? |
What do you suggest |
Let's count really existing items of query result in php instead of SQL. It should help (It's a guess, I don't know) |
But this WONT fix this @Izopi4a The only way is to do: SELECT COUNT(*) FROM (
-- OUR SELECT STATEMENT WHICH WILL RETURN THIS ROWS
) But phalcon dont support queries like this. Perhaps maybe - if we have having, then get builded sql query, and do raw query as i wrote above ? |
Have the same problem. When using HAVING in QueryBuilder, pagination execute wrong query. That's why we have to use subquery for true pagination results as @Jurigag wrote above. |
As i wrote. Pagination executes PROPER QUERY. Just having works AFTER selecting, count etc. This is not a bug as it is. This is just how sql works. The only solution to this is what i wrote above. |
Well, i am trying to fix this, but there is a little problem, it would be to heavy operation to figure out which columns to select imho, like get primary keys, figure out alias, user columnMap etc etc, much simpler and faster will be just adding property columns where user will provide for example "Robots.id" and this column will be used for: SELECT COUNT(*) FROM {
SELECT THIS COLUMNS FROM MODEL {
} as T1
} Don't see really better idea, adding like auomatic way to making which columns we only need for selecting count would be way too heavy operation(get model instances, get first model instance, figure out alias, figure out primary keys, check against column map). We can't use really columns from original query builder cuz it will select pretty much everything from database. Anyway i highly doubt that it will have good performance anyway. |
i still beleave my idea should be fine, simply because it will be up to the developer to handle his stuff |
Hmmm, maybe it can be fine too, i don't know really, it's not hard to do it like my idea, but still developer would need provide columns for count select. |
i absolutely agree with you, i am just afraid that implementing such a dynamic options for pagination might become too complicated / complex to people to use them. It just orm should be "1 liner" in my mind, thats all But i dont mind, if someone come up with a plan we can agree who to cook it up |
I mean something like: 'builder' => $builder, // some builder with Robots aliast and RobotsParts alias
'limit' => 20,
'page' => $currentPage,
'columns' => 'Robots.id' And then for getting count there will be query: SELECT COUNT(*) FROM {
SELECT Robots.id FROM // our columns option
// the rest of query from builder
} as T1 Any dynamic way to figure out this columns for this nested select will cause slowdown really. |
This thing should be consider as fix or new feature? |
Here is my PR for this thing, let me know what you think. |
This can be closed. It's implemented in 3.2.x |
Using
having
of a query builder forces it's paginator to show total pages and total items as if there was nohaving
set.Issue confirmed in 2.1.x and 3.0.0 on windows 7, php 5.6
How to reproduce:
/bootstrap.php
/model/a.php:
sql database dump:
Expected result:
Query builder result:
The text was updated successfully, but these errors were encountered: