1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Mado\QueryBundle\Objects\MetaDataAdapter; |
7
|
|
|
use Mado\QueryBundle\Queries\QueryBuilderOptions; |
8
|
|
|
use Mado\QueryBundle\Services\StringParser; |
9
|
|
|
|
10
|
|
|
abstract class AbstractQuery |
11
|
|
|
{ |
12
|
|
|
protected $manager; |
13
|
|
|
|
14
|
|
|
protected $entityName; |
15
|
|
|
|
16
|
|
|
protected $entityAlias; |
17
|
|
|
|
18
|
|
|
protected $parser; |
19
|
|
|
|
20
|
|
|
protected $qBuilder; |
21
|
|
|
|
22
|
50 |
|
public function __construct(EntityManager $manager) |
23
|
|
|
{ |
24
|
50 |
|
$this->manager = $manager; |
25
|
50 |
|
$this->parser = new StringParser(); |
26
|
50 |
|
} |
27
|
|
|
|
28
|
|
|
public function createSelectAndGroupBy($entityName, $alias, $groupByField) |
29
|
|
|
{ |
30
|
|
|
$select = $alias . '.' . $groupByField . ', count(' . $alias . '.id) as num'; |
31
|
|
|
$groupBy = $alias . '.' . $groupByField . ''; |
32
|
|
|
|
33
|
|
|
$this->entityName = $entityName; |
34
|
|
|
$this->entityAlias = $alias; |
35
|
|
|
|
36
|
|
|
$this->qBuilder = $this->manager->createQueryBuilder() |
37
|
|
|
->select($select) |
38
|
|
|
->groupBy($groupBy); |
39
|
|
|
} |
40
|
|
|
|
41
|
25 |
|
public function createQueryBuilder($entityName, $alias) |
42
|
|
|
{ |
43
|
25 |
|
$this->entityName = $entityName; |
44
|
25 |
|
$this->entityAlias = $alias; |
45
|
|
|
|
46
|
25 |
|
$this->qBuilder = $this->manager->createQueryBuilder() |
47
|
25 |
|
->select($alias) |
48
|
25 |
|
->from($this->entityName, $alias); |
49
|
25 |
|
} |
50
|
|
|
|
51
|
11 |
|
public function getEntityName() |
52
|
|
|
{ |
53
|
11 |
|
return $this->entityName; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function loadMetadataAndOptions( |
57
|
|
|
MetaDataAdapter $metadata, |
58
|
|
|
QueryBuilderOptions $options |
59
|
|
|
) { |
60
|
|
|
$this->setFields($metadata->getFields()); |
61
|
|
|
|
62
|
|
|
$this->setAndFilters($options->getAndFilters()); |
63
|
|
|
$this->setOrFilters($options->getOrFilters()); |
64
|
|
|
$this->setSorting($options->getSorting()); |
65
|
|
|
$this->setRel([$options->getRel()]); |
66
|
|
|
$this->setPrinting($options->getPrinting()); |
67
|
|
|
$this->setSelect($options->getSelect()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
abstract public function setFields(array $fields = []); |
71
|
|
|
abstract public function setAndFilters(array $andFilters = []); |
72
|
|
|
abstract public function setOrFilters(array $orFilters = []); |
73
|
|
|
abstract public function setSorting(array $sorting = []); |
74
|
|
|
abstract public function setRel(array $rel); |
75
|
|
|
abstract public function setPrinting($printing); |
76
|
|
|
abstract public function setSelect($select); |
77
|
|
|
} |
78
|
|
|
|