1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace AtDataGrid\DataSource\Doctrine;
|
4
|
|
|
|
5
|
|
|
use AtDataGrid\DataSource\AbstractDataSource;
|
6
|
|
|
use AtDataGrid\Column;
|
7
|
|
|
use AtDataGrid\Filter\Doctrine2Filter;
|
8
|
|
|
use Doctrine\ORM\EntityManager;
|
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator;
|
10
|
|
|
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
|
11
|
|
|
|
12
|
|
|
class QueryBuilder extends AbstractDataSource
|
13
|
|
|
{
|
14
|
|
|
/**
|
15
|
|
|
* @var EntityManager
|
16
|
|
|
*/
|
17
|
|
|
protected $em;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var \Doctrine\ORM\QueryBuilder
|
21
|
|
|
*/
|
22
|
|
|
protected $qb;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @var string
|
26
|
|
|
*/
|
27
|
|
|
protected $entityName;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @param EntityManager $em
|
31
|
|
|
* @param $entityName
|
32
|
|
|
*/
|
33
|
|
|
public function __construct(EntityManager $em, $entityName)
|
34
|
|
|
{
|
35
|
|
|
$this->em = $em;
|
36
|
|
|
$this->qb = $this->em->createQueryBuilder();
|
37
|
|
|
$this->entityName = $entityName;
|
38
|
|
|
$this->qb->select('f')->from($entityName, 'f');
|
39
|
|
|
|
40
|
|
|
$this->paginatorAdapter = new DoctrinePaginator(new Paginator($this->qb));
|
|
|
|
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* @return EntityManager
|
45
|
|
|
*/
|
46
|
|
|
public function getEntityManager()
|
47
|
|
|
{
|
48
|
|
|
return $this->em;
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* @return \Doctrine\ORM\QueryBuilder
|
53
|
|
|
*/
|
54
|
|
|
public function getQueryBuilder()
|
55
|
|
|
{
|
56
|
|
|
return $this->qb;
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* @return array
|
61
|
|
|
*/
|
62
|
|
|
public function loadColumns()
|
63
|
|
|
{
|
64
|
|
|
$columns = [];
|
65
|
|
|
$classMetadata = $this->em->getClassMetadata($this->entityName);
|
66
|
|
|
$baseTableColumns = $classMetadata->getFieldNames();
|
67
|
|
|
|
68
|
|
|
// Setup default settings for base table column fields
|
69
|
|
|
foreach ($baseTableColumns as $columnName) {
|
70
|
|
|
$columnDataType = $classMetadata->getTypeOfField($columnName);
|
71
|
|
|
|
72
|
|
|
$this->tableColumns[] = $columnName;
|
|
|
|
|
73
|
|
|
|
74
|
|
|
// @todo Move it to separate class
|
75
|
|
|
switch (true) {
|
76
|
|
|
case in_array($columnDataType, array('datetime', 'timestamp', 'time')):
|
77
|
|
|
$column = new Column\DateTime($columnName);
|
78
|
|
|
break;
|
79
|
|
|
|
80
|
|
|
case in_array($columnDataType, array('date', 'year')):
|
81
|
|
|
$column = new Column\Date($columnName);
|
82
|
|
|
break;
|
83
|
|
|
|
84
|
|
|
case in_array($columnDataType, array('mediumtext', 'text', 'longtext')):
|
85
|
|
|
$column = new Column\Textarea($columnName);
|
86
|
|
|
break;
|
87
|
|
|
|
88
|
|
|
default:
|
89
|
|
|
$column = new Column\Literal($columnName);
|
90
|
|
|
break;
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
$column->setLabel($columnName);
|
94
|
|
|
|
95
|
|
|
$columns[$classMetadata->getColumnName($columnName)] = $column;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
$joinedColumns = $classMetadata->getAssociationNames();
|
99
|
|
|
foreach ($joinedColumns as $columnName) {
|
100
|
|
|
$column = new Column\Literal($classMetadata->getSingleAssociationJoinColumnName($columnName));
|
101
|
|
|
$column->setLabel($columnName);
|
102
|
|
|
|
103
|
|
|
$columns[$classMetadata->getSingleAssociationJoinColumnName($columnName)] = $column;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
return $columns;
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* @param $order
|
111
|
|
|
* @param array $filters
|
112
|
|
|
* @return $this|mixed
|
113
|
|
|
*/
|
114
|
|
|
public function prepare($order, $filters = [])
|
115
|
|
|
{
|
116
|
|
|
/**
|
117
|
|
|
* Filtering
|
118
|
|
|
*/
|
119
|
|
|
foreach ($filters as $columnName => $filter) {
|
120
|
|
|
if (!$filter instanceof Doctrine2Filter) {
|
|
|
|
|
121
|
|
|
throw new \RuntimeException('Doctrine/QueryBuilder data source requires Filter\Doctrine filters');
|
122
|
|
|
}
|
123
|
|
|
$filter->apply($this->getQueryBuilder(), $columnName, $filter->getValue());
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
/**
|
127
|
|
|
* Sorting
|
128
|
|
|
*/
|
129
|
|
|
/* if ($order) {
|
|
|
|
|
130
|
|
|
$orderParts = explode(' ', $order);
|
131
|
|
|
if (in_array($orderParts[0], $this->tableColumns)) {
|
132
|
|
|
$order = $this->getTableGateway()->getTable() . '.' . $order;
|
133
|
|
|
}
|
134
|
|
|
$this->getSelect()->order($order);
|
135
|
|
|
}*/
|
136
|
|
|
|
137
|
|
|
$this->getEventManager()->trigger(self::EVENT_DATASOURCE_PREPARE_POST, $this->getQueryBuilder());
|
138
|
|
|
|
139
|
|
|
//var_dump($this->getSelect()->getSqlString());exit;
|
|
|
|
|
140
|
|
|
return $this;
|
141
|
|
|
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
/**
|
145
|
|
|
* Return row by identifier (primary key)
|
146
|
|
|
*
|
147
|
|
|
* @param $key
|
148
|
|
|
* @return array|mixed
|
149
|
|
|
*/
|
150
|
|
|
public function find($key)
|
151
|
|
|
{
|
152
|
|
|
}
|
153
|
|
|
|
154
|
|
|
/**
|
155
|
|
|
* @param $data
|
156
|
|
|
* @return int|mixed
|
157
|
|
|
*/
|
158
|
|
|
public function insert($data)
|
159
|
|
|
{
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
/**
|
163
|
|
|
* @param $data
|
164
|
|
|
* @param $key
|
165
|
|
|
* @return int|mixed
|
166
|
|
|
*/
|
167
|
|
|
public function update($data, $key)
|
168
|
|
|
{
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
/**
|
172
|
|
|
* @param $key
|
173
|
|
|
* @return int|mixed
|
174
|
|
|
*/
|
175
|
|
|
public function delete($key)
|
176
|
|
|
{
|
177
|
|
|
}
|
178
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..