1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Source; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
|
|
|
6
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
|
|
|
|
7
|
|
|
use Dtc\GridBundle\Grid\Column\GridColumn; |
8
|
|
|
|
9
|
|
|
class DocumentGridSource extends AbstractDoctrineGridSource |
10
|
|
|
{ |
11
|
|
|
protected $repository; |
12
|
|
|
protected $findCache; |
13
|
|
|
|
14
|
|
|
public function __construct(DocumentManager $documentManager, $documentName) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($documentManager, $documentName); |
17
|
|
|
$this->repository = $documentManager->getRepository($documentName); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return \Doctrine\ODM\MongoDB\Query\Builder |
|
|
|
|
22
|
|
|
* |
23
|
|
|
* @throws \Exception |
24
|
|
|
*/ |
25
|
|
|
protected function getQueryBuilder() |
26
|
|
|
{ |
27
|
|
|
if (!$this->objectManager instanceof DocumentManager) { |
28
|
|
|
throw new \Exception("Should be DocumentManager, instead it's ".get_class($this->objectManager)); |
29
|
|
|
} |
30
|
|
|
$qb = $this->objectManager->createQueryBuilder($this->objectName); |
31
|
|
|
|
32
|
|
|
/** @var ClassMetadata $classMetaData */ |
33
|
|
|
$classMetaData = $this->getClassMetadata(); |
34
|
|
|
$classFields = $classMetaData->fieldMappings; |
35
|
|
|
|
36
|
|
|
$columns = $this->getColumns(); |
37
|
|
|
$fieldList = []; |
38
|
|
|
foreach ($columns as $column) { |
39
|
|
|
if ($column instanceof GridColumn && $column->isSearchable()) { |
40
|
|
|
$fieldList[$column->getField()] = true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
if ($this->filter) { |
|
|
|
|
44
|
|
|
$validFilters = array_intersect_key($this->filter, $classFields); |
45
|
|
|
|
46
|
|
|
$query = []; |
47
|
|
|
foreach ($validFilters as $key => $value) { |
48
|
|
|
if (isset($fieldList[$key])) { |
49
|
|
|
if (is_array($value)) { |
50
|
|
|
$qb->field($key)->in($value); |
51
|
|
|
} else { |
52
|
|
|
$qb->field($key)->equals($value); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
if (!$query) { |
|
|
|
|
57
|
|
|
$starFilter = array_intersect_key($this->filter, ['*' => null]); |
58
|
|
|
if ($starFilter) { |
|
|
|
|
59
|
|
|
$value = current($starFilter); |
60
|
|
|
foreach ($classFields as $key => $info) { |
61
|
|
|
if (isset($fieldList[$key])) { |
62
|
|
|
$expr = $qb->expr()->field($key); |
63
|
|
|
switch ($info['type']) { |
64
|
|
|
case 'integer': |
65
|
|
|
$expr = $expr->equals(intval($value)); |
66
|
|
|
break; |
67
|
|
|
default: |
68
|
|
|
$expr = $expr->equals($value); |
69
|
|
|
} |
70
|
|
|
$qb->addOr($expr); |
71
|
|
|
} |
72
|
|
|
// @TODO - maybe allow pattern searches some day: new \MongoRegex('/.*'.$value.'.*/') |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
if ($this->orderBy) { |
|
|
|
|
78
|
|
|
foreach ($this->orderBy as $key => $direction) { |
79
|
|
|
$qb->sort($key, $direction); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$qb->limit($this->limit); |
84
|
|
|
$qb->skip($this->offset); |
85
|
|
|
|
86
|
|
|
return $qb; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return mixed |
91
|
|
|
* |
92
|
|
|
* @throws \Exception |
93
|
|
|
*/ |
94
|
|
|
public function getClassMetadata() |
95
|
|
|
{ |
96
|
|
|
$metaFactory = $this->objectManager->getMetadataFactory(); |
97
|
|
|
$classInfo = $metaFactory->getMetadataFor($this->objectName); |
98
|
|
|
|
99
|
|
|
return $classInfo; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return mixed |
104
|
|
|
* |
105
|
|
|
* @throws \Doctrine\ODM\MongoDB\MongoDBException |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
|
|
public function getCount() |
109
|
|
|
{ |
110
|
|
|
$result = $this->getQueryBuilder()->limit(0)->skip(0)->count()->getQuery()->execute(); |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
117
|
|
|
* |
118
|
|
|
* @throws \Doctrine\ODM\MongoDB\MongoDBException |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
public function getRecords() |
122
|
|
|
{ |
123
|
|
|
return $this->getQueryBuilder()->getQuery()->execute()->toArray(false); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $id |
128
|
|
|
* |
129
|
|
|
* @return mixed|null |
130
|
|
|
* |
131
|
|
|
* @throws \Exception |
132
|
|
|
*/ |
133
|
|
|
public function find($id) |
134
|
|
|
{ |
135
|
|
|
if (!$this->objectManager instanceof DocumentManager) { |
136
|
|
|
throw new \Exception('should be DocumentManager, instead'.get_class($this->objectManager)); |
137
|
|
|
} |
138
|
|
|
if (!$this->hasIdColumn()) { |
139
|
|
|
throw new \Exception('No id column found for '.$this->objectName); |
140
|
|
|
} |
141
|
|
|
$qb = $this->objectManager->createQueryBuilder($this->objectName); |
142
|
|
|
$qb->field($this->idColumn)->equals($id); |
143
|
|
|
$result = $qb->getQuery()->execute()->toArray(false); |
144
|
|
|
if (isset($result[0])) { |
145
|
|
|
return $result[0]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $id |
151
|
|
|
* @param bool $soft |
152
|
|
|
* @param string $softColumn |
153
|
|
|
* @param string $softColumnType |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
* |
157
|
|
|
* @throws \Exception |
158
|
|
|
*/ |
159
|
|
|
public function remove($id, $soft = false, $softColumn = 'deletedAt', $softColumnType = 'datetime') |
160
|
|
|
{ |
161
|
|
|
if (!$this->hasIdColumn()) { |
162
|
|
|
throw new \Exception('No id column found for '.$this->objectName); |
163
|
|
|
} |
164
|
|
|
$repository = $this->objectManager->getRepository($this->objectName); |
165
|
|
|
$document = $repository->find($id); |
166
|
|
|
if ($document) { |
167
|
|
|
$this->objectManager->remove($document); |
168
|
|
|
$this->objectManager->flush(); |
169
|
|
|
|
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths