@@ -37,27 +37,31 @@ public function walkSelectStatement(SelectStatement $selectStatement): void
37
37
throw new RuntimeException ('Cannot count query which selects two FROM components, cannot make distinction ' );
38
38
}
39
39
40
- $ fromRoot = reset ($ from );
41
- $ rootAlias = $ fromRoot ->rangeVariableDeclaration ->aliasIdentificationVariable ;
42
- $ rootClass = $ this ->getMetadataForDqlAlias ($ rootAlias );
43
- $ identifierFieldName = $ rootClass ->getSingleIdentifierFieldName ();
40
+ $ distinct = $ this ->_getQuery ()->getHint (self ::HINT_DISTINCT );
44
41
45
- $ pathType = PathExpression::TYPE_STATE_FIELD ;
46
- if (isset ($ rootClass ->associationMappings [$ identifierFieldName ])) {
47
- $ pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION ;
48
- }
42
+ $ countPathExpressionOrLiteral = '* ' ;
43
+ if ($ distinct ) {
44
+ $ fromRoot = reset ($ from );
45
+ $ rootAlias = $ fromRoot ->rangeVariableDeclaration ->aliasIdentificationVariable ;
46
+ $ rootClass = $ this ->getMetadataForDqlAlias ($ rootAlias );
47
+ $ identifierFieldName = $ rootClass ->getSingleIdentifierFieldName ();
48
+
49
+ $ pathType = PathExpression::TYPE_STATE_FIELD ;
50
+ if (isset ($ rootClass ->associationMappings [$ identifierFieldName ])) {
51
+ $ pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION ;
52
+ }
49
53
50
- $ pathExpression = new PathExpression (
51
- PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION ,
52
- $ rootAlias ,
53
- $ identifierFieldName ,
54
- );
55
- $ pathExpression ->type = $ pathType ;
54
+ $ countPathExpressionOrLiteral = new PathExpression (
55
+ PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION ,
56
+ $ rootAlias ,
57
+ $ identifierFieldName ,
58
+ );
59
+ $ countPathExpressionOrLiteral ->type = $ pathType ;
60
+ }
56
61
57
- $ distinct = $ this ->_getQuery ()->getHint (self ::HINT_DISTINCT );
58
62
$ selectStatement ->selectClause ->selectExpressions = [
59
63
new SelectExpression (
60
- new AggregateExpression ('count ' , $ pathExpression , $ distinct ),
64
+ new AggregateExpression ('count ' , $ countPathExpressionOrLiteral , $ distinct ),
61
65
null ,
62
66
),
63
67
];
0 commit comments