|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Mado\QueryBundle\Services\StringParser; |
|
7
|
|
|
|
|
8
|
|
|
class AbstractQuery |
|
9
|
|
|
{ |
|
10
|
|
|
protected $manager; |
|
11
|
|
|
|
|
12
|
|
|
protected $entityName; |
|
13
|
|
|
|
|
14
|
|
|
protected $entityAlias; |
|
15
|
|
|
|
|
16
|
|
|
protected $parser; |
|
17
|
|
|
|
|
18
|
|
|
protected $qBuilder; |
|
19
|
|
|
|
|
20
|
|
|
/** @since version 2.3 */ |
|
21
|
|
|
protected $options; |
|
22
|
|
|
|
|
23
|
28 |
|
public function __construct(EntityManager $manager) |
|
24
|
|
|
{ |
|
25
|
28 |
|
$this->manager = $manager; |
|
26
|
28 |
|
$this->parser = new StringParser(); |
|
27
|
28 |
|
$this->options = QueryBuilderOptions::fromArray([]); |
|
28
|
28 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function createSelectAndGroupBy($entityName, $alias, $groupByField) |
|
31
|
|
|
{ |
|
32
|
|
|
$select = $alias . '.' . $groupByField . ', count(' . $alias . '.id) as num'; |
|
33
|
|
|
$groupBy = $alias . '.' . $groupByField . ''; |
|
34
|
|
|
|
|
35
|
|
|
$this->entityName = $entityName; |
|
36
|
|
|
$this->entityAlias = $alias; |
|
37
|
|
|
|
|
38
|
|
|
$this->qBuilder = $this->manager->createQueryBuilder($this->entityName) |
|
|
|
|
|
|
39
|
|
|
->select($select) |
|
40
|
|
|
->groupBy($groupBy); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
public function createQueryBuilder($entityName, $alias) |
|
44
|
|
|
{ |
|
45
|
6 |
|
$this->entityName = $entityName; |
|
46
|
6 |
|
$this->entityAlias = $alias; |
|
47
|
|
|
|
|
48
|
6 |
|
$this->qBuilder = $this->manager->createQueryBuilder($this->entityName) |
|
|
|
|
|
|
49
|
6 |
|
->select($alias) |
|
50
|
6 |
|
->from($this->entityName, $alias); |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function getEntityName() |
|
54
|
|
|
{ |
|
55
|
3 |
|
return $this->entityName; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @since version 2.3 */ |
|
59
|
|
|
public function getEntityManager() : EntityManager |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->manager; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @since version 2.3 */ |
|
65
|
2 |
|
public function ensureQueryBuilderIsDefined() |
|
66
|
|
|
{ |
|
67
|
2 |
|
if (!$this->qBuilder) { |
|
68
|
1 |
|
throw new \RuntimeException( |
|
69
|
|
|
'Oops! QueryBuilder was never initialized. ' |
|
70
|
|
|
. "\n" . 'QueryBuilderFactory::createQueryBuilder()' |
|
71
|
1 |
|
. "\n" . 'QueryBuilderFactory::createSelectAndGroupBy()' |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
1 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.