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

Count with Group by fails #502

Open
Netzhirsch opened this issue Sep 6, 2018 · 6 comments
Open

Count with Group by fails #502

Netzhirsch opened this issue Sep 6, 2018 · 6 comments

Comments

@Netzhirsch
Copy link

Netzhirsch commented Sep 6, 2018

I have the following query:

$qb = $em->createQueryBuilder();
$qb->select('de', 'COUNT(de.auftragsnummer) as menge');
$qb->from('App\Entity\DispositionsplanEintrag', 'de');
$qb->where('de.geloescht IS NULL');
$qb->groupBy('de.auftragsnummer');
$qb->orderBy('de.auftragsnummer', 'ASC');

Executed by the following code:

$query = $qb->getQuery();
$paginator = $this->get('knp_paginator');
$eintraege = $paginator->paginate($query, $request->query->getInt('page', 1), 20);

it results in having the variable "menge" always has the value 1.
if I execute $eintraege = $query->getResult(); then the value of "menge" is correct.
Is there anything wrong in my example, or is it a bug?

Thank you!

@nicolasmure
Copy link
Contributor

Hello,
Can you also post your configuration please ? Do you have distinct: true ? (see config).
Also, you can pretty print your github issue with markdown syntax highlighting : https://guides.github.com/features/mastering-markdown/

@Netzhirsch
Copy link
Author

Netzhirsch commented Sep 6, 2018

Yes, distinct is set to true:

page_range: 3
    default_options:
        page_name: page
        sort_field_name: sort
        sort_direction_name: direction
        distinct: true
        filter_field_name: filterField
        filter_value_name: filterValue
    template:                                        
        pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig'                           
        sortable: '@KnpPaginator/Pagination/sortable_link.html.twig'                            
        filtration: '@KnpPaginator/Pagination/filtration.html.twig'

@nicolasmure
Copy link
Contributor

nicolasmure commented Sep 6, 2018

Have you took a look to https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/manual_counting.md ?

And have you tried
$pagination = $paginator->paginate($query, $page, $items_per_page, ['wrap-queries' => true]); ?

@Netzhirsch
Copy link
Author

Yes, as well. Manual counting gives me only a count for the whole query, but I need it for every row. If 3 rows are grouped, the variable "menge" has the value 3.
wrap-queries didn't help me either.

@nicolasmure
Copy link
Contributor

do you mind to translate the german var names into english please ? it'll ease the understanding ;)

@Netzhirsch
Copy link
Author

Here is the difference:
Executed with $eintraege = $query->getResult();:

array:5 [▼
  0 => array:2 [▼
    0 => DispositionsplanEintrag {#929 ▶}
    "menge" => "4"
  ]
  1 => array:2 [▼
    0 => DispositionsplanEintrag {#1038 ▶}
    "menge" => "1"
  ]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
]

With $eintraege = $paginator->paginate($query, $request->query->getInt('page', 1), 20);:

SlidingPagination {#1236 ▼
  -route: "speditionsbuch_liste"
  -params: []
  -pageRange: 3
  -template: "@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig"
  -sortableTemplate: "@KnpPaginator/Pagination/sortable_link.html.twig"
  -filtrationTemplate: "@KnpPaginator/Pagination/filtration.html.twig"
  #currentPageNumber: 1
  #numItemsPerPage: 20
  #items: array:5 [▼
    0 => array:2 [▼
      0 => DispositionsplanEintrag {#1402 ▶}
      "menge" => "1"
    ]
    1 => array:2 [▼
      0 => DispositionsplanEintrag {#1543 ▶}
      "menge" => "1"
    ]
    2 => array:2 [▶]
    3 => array:2 [▶]
    4 => array:2 [▶]
  ]
  #totalCount: 8
  #paginatorOptions: array:6 [▶]
  #customParameters: array:1 [▶]
}

"menge" means "amount" or "count" ;)

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

No branches or pull requests

1 participant