|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineMongoDBAdminBundle\Model; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata as CommonClassMetadata; |
|
18
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
|
19
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
|
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
|
21
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridInterface; |
|
22
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
23
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
|
24
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
|
25
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; |
|
26
|
|
|
use Sonata\Exporter\Source\DoctrineODMQuerySourceIterator; |
|
27
|
|
|
use Symfony\Bridge\Doctrine\ManagerRegistry; |
|
28
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
29
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
30
|
|
|
|
|
31
|
|
|
class ModelManager implements ModelManagerInterface |
|
32
|
|
|
{ |
|
33
|
|
|
public const ID_SEPARATOR = '-'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ManagerRegistry |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $registry; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var PropertyAccessorInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $propertyAccessor; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* NEXT_MAJOR: Make $propertyAccessor mandatory. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(ManagerRegistry $registry, ?PropertyAccessorInterface $propertyAccessor = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->registry = $registry; |
|
51
|
|
|
|
|
52
|
|
|
// NEXT_MAJOR: Remove this block. |
|
53
|
|
|
if (!$propertyAccessor instanceof PropertyAccessorInterface) { |
|
54
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
55
|
|
|
'Not passing an object implementing "%s" as argument 2 for "%s()" is deprecated since' |
|
56
|
|
|
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will throw a %s error in 4.0.', |
|
57
|
|
|
PropertyAccessorInterface::class, |
|
58
|
|
|
__METHOD__, |
|
59
|
|
|
\TypeError::class |
|
60
|
|
|
), E_USER_DEPRECATED); |
|
61
|
|
|
|
|
62
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->propertyAccessor = $propertyAccessor; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getMetadata($class) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getDocumentManager($class)->getMetadataFactory()->getMetadataFor($class); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the model's metadata holding the fully qualified property, and the last |
|
75
|
|
|
* property name. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $baseClass The base class of the model holding the fully qualified property |
|
78
|
|
|
* @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
|
79
|
|
|
* property string) |
|
80
|
|
|
* |
|
81
|
|
|
* @return array( |
|
|
|
|
|
|
82
|
|
|
* \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentMetadata, |
|
83
|
|
|
* string $lastPropertyName, |
|
84
|
|
|
* array $parentAssociationMappings |
|
85
|
|
|
* ) |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getParentMetadataForProperty($baseClass, $propertyFullName) |
|
88
|
|
|
{ |
|
89
|
|
|
$nameElements = explode('.', $propertyFullName); |
|
90
|
|
|
$lastPropertyName = array_pop($nameElements); |
|
91
|
|
|
$class = $baseClass; |
|
92
|
|
|
$parentAssociationMappings = []; |
|
93
|
|
|
|
|
94
|
|
|
foreach ($nameElements as $nameElement) { |
|
95
|
|
|
$metadata = $this->getMetadata($class); |
|
96
|
|
|
$parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; |
|
97
|
|
|
$class = $metadata->getAssociationTargetClass($nameElement); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return [$this->getMetadata($class), $lastPropertyName, $parentAssociationMappings]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function hasMetadata($class) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getDocumentManager($class)->getMetadataFactory()->hasMetadataFor($class); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getNewFieldDescriptionInstance($class, $name, array $options = []) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!\is_string($name)) { |
|
111
|
|
|
throw new \RuntimeException('The name argument must be a string'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (!isset($options['route']['name'])) { |
|
115
|
|
|
$options['route']['name'] = 'edit'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (!isset($options['route']['parameters'])) { |
|
119
|
|
|
$options['route']['parameters'] = []; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
[$metadata, $propertyName, $parentAssociationMappings] = $this->getParentMetadataForProperty($class, $name); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$fieldDescription = new FieldDescription(); |
|
125
|
|
|
$fieldDescription->setName($name); |
|
126
|
|
|
$fieldDescription->setOptions($options); |
|
127
|
|
|
$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
|
128
|
|
|
|
|
129
|
|
|
/* @var ClassMetadata */ |
|
130
|
|
|
if (isset($metadata->associationMappings[$propertyName])) { |
|
131
|
|
|
$fieldDescription->setAssociationMapping($metadata->associationMappings[$propertyName]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (isset($metadata->fieldMappings[$propertyName])) { |
|
135
|
|
|
$fieldDescription->setFieldMapping($metadata->fieldMappings[$propertyName]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $fieldDescription; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function create($object): void |
|
142
|
|
|
{ |
|
143
|
|
|
$documentManager = $this->getDocumentManager($object); |
|
144
|
|
|
$documentManager->persist($object); |
|
145
|
|
|
$documentManager->flush(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function update($object): void |
|
149
|
|
|
{ |
|
150
|
|
|
$documentManager = $this->getDocumentManager($object); |
|
151
|
|
|
$documentManager->persist($object); |
|
152
|
|
|
$documentManager->flush(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function delete($object): void |
|
156
|
|
|
{ |
|
157
|
|
|
$documentManager = $this->getDocumentManager($object); |
|
158
|
|
|
$documentManager->remove($object); |
|
159
|
|
|
$documentManager->flush(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function find($class, $id) |
|
163
|
|
|
{ |
|
164
|
|
|
if (!isset($id)) { |
|
165
|
|
|
return null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$documentManager = $this->getDocumentManager($class); |
|
169
|
|
|
|
|
170
|
|
|
if (is_numeric($id)) { |
|
171
|
|
|
$value = $documentManager->getRepository($class)->find((int) $id); |
|
172
|
|
|
|
|
173
|
|
|
if (!empty($value)) { |
|
174
|
|
|
return $value; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $documentManager->getRepository($class)->find($id); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function findBy($class, array $criteria = []) |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->getDocumentManager($class)->getRepository($class)->findBy($criteria); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function findOneBy($class, array $criteria = []) |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->getDocumentManager($class)->getRepository($class)->findOneBy($criteria); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param object|string $class |
|
193
|
|
|
* |
|
194
|
|
|
* @throw \RuntimeException |
|
195
|
|
|
* |
|
196
|
|
|
* @return \Doctrine\ODM\MongoDB\DocumentManager |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getDocumentManager($class) |
|
199
|
|
|
{ |
|
200
|
|
|
if (\is_object($class)) { |
|
201
|
|
|
$class = \get_class($class); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$dm = $this->registry->getManagerForClass($class); |
|
205
|
|
|
|
|
206
|
|
|
if (!$dm) { |
|
207
|
|
|
throw new \RuntimeException(sprintf('No document manager defined for class %s', $class)); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $dm; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function getParentFieldDescription($parentAssociationMapping, $class) |
|
214
|
|
|
{ |
|
215
|
|
|
$fieldName = $parentAssociationMapping['fieldName']; |
|
216
|
|
|
|
|
217
|
|
|
$metadata = $this->getMetadata($class); |
|
218
|
|
|
|
|
219
|
|
|
$associatingMapping = $metadata->associationMappings[$parentAssociationMapping]; |
|
220
|
|
|
|
|
221
|
|
|
$fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName); |
|
222
|
|
|
$fieldDescription->setName($parentAssociationMapping); |
|
|
|
|
|
|
223
|
|
|
$fieldDescription->setAssociationMapping($associatingMapping); |
|
224
|
|
|
|
|
225
|
|
|
return $fieldDescription; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function createQuery($class, $alias = 'o') |
|
229
|
|
|
{ |
|
230
|
|
|
$repository = $this->getDocumentManager($class)->getRepository($class); |
|
231
|
|
|
|
|
232
|
|
|
return new ProxyQuery($repository->createQueryBuilder()); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function executeQuery($query) |
|
236
|
|
|
{ |
|
237
|
|
|
if ($query instanceof Builder) { |
|
238
|
|
|
return $query->getQuery()->execute(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $query->execute(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function getModelIdentifier($class) |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->getMetadata($class)->identifier; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function getIdentifierValues($document) |
|
250
|
|
|
{ |
|
251
|
|
|
return [$this->getDocumentManager($document)->getUnitOfWork()->getDocumentIdentifier($document)]; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function getIdentifierFieldNames($class) |
|
255
|
|
|
{ |
|
256
|
|
|
return [$this->getMetadata($class)->getIdentifier()]; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function getNormalizedIdentifier($document) |
|
260
|
|
|
{ |
|
261
|
|
|
if (null === $document) { |
|
262
|
|
|
return null; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if (!\is_object($document)) { |
|
266
|
|
|
throw new \RuntimeException('Invalid argument, object or null required'); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
// the document is not managed |
|
270
|
|
|
if (!$this->getDocumentManager($document)->contains($document)) { |
|
271
|
|
|
return null; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$values = $this->getIdentifierValues($document); |
|
275
|
|
|
|
|
276
|
|
|
return implode(self::ID_SEPARATOR, $values); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function getUrlSafeIdentifier($document) |
|
280
|
|
|
{ |
|
281
|
|
|
return $this->getNormalizedIdentifier($document); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx): void |
|
285
|
|
|
{ |
|
286
|
|
|
$queryBuilder = $queryProxy->getQueryBuilder(); |
|
287
|
|
|
$queryBuilder->field('_id')->in($idx); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
public function batchDelete($class, ProxyQueryInterface $queryProxy): void |
|
291
|
|
|
{ |
|
292
|
|
|
/** @var Query $queryBuilder */ |
|
293
|
|
|
$queryBuilder = $queryProxy->getQuery(); |
|
294
|
|
|
|
|
295
|
|
|
$documentManager = $this->getDocumentManager($class); |
|
296
|
|
|
|
|
297
|
|
|
$i = 0; |
|
298
|
|
|
foreach ($queryBuilder->execute() as $object) { |
|
299
|
|
|
$documentManager->remove($object); |
|
300
|
|
|
|
|
301
|
|
|
if (0 === (++$i % 20)) { |
|
302
|
|
|
$documentManager->flush(); |
|
303
|
|
|
$documentManager->clear(); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$documentManager->flush(); |
|
308
|
|
|
$documentManager->clear(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
|
312
|
|
|
{ |
|
313
|
|
|
$datagrid->buildPager(); |
|
314
|
|
|
$query = $datagrid->getQuery(); |
|
315
|
|
|
|
|
316
|
|
|
$query->setFirstResult($firstResult); |
|
317
|
|
|
$query->setMaxResults($maxResult); |
|
318
|
|
|
|
|
319
|
|
|
return new DoctrineODMQuerySourceIterator($query instanceof ProxyQuery ? $query->getQuery() : $query, $fields); |
|
|
|
|
|
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function getExportFields($class) |
|
323
|
|
|
{ |
|
324
|
|
|
$metadata = $this->getDocumentManager($class)->getClassMetadata($class); |
|
325
|
|
|
|
|
326
|
|
|
return $metadata->getFieldNames(); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
public function getModelInstance($class) |
|
330
|
|
|
{ |
|
331
|
|
|
if (!class_exists($class)) { |
|
332
|
|
|
throw new \InvalidArgumentException(sprintf('Class "%s" not found', $class)); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$r = new \ReflectionClass($class); |
|
336
|
|
|
if ($r->isAbstract()) { |
|
337
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot initialize abstract class: %s', $class)); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
$constructor = $r->getConstructor(); |
|
341
|
|
|
|
|
342
|
|
|
if (null !== $constructor && (!$constructor->isPublic() || $constructor->getNumberOfRequiredParameters() > 0)) { |
|
343
|
|
|
return $r->newInstanceWithoutConstructor(); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
return new $class(); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
|
350
|
|
|
{ |
|
351
|
|
|
$values = $datagrid->getValues(); |
|
352
|
|
|
|
|
353
|
|
|
if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) { |
|
354
|
|
|
if ('ASC' === $values['_sort_order']) { |
|
355
|
|
|
$values['_sort_order'] = 'DESC'; |
|
356
|
|
|
} else { |
|
357
|
|
|
$values['_sort_order'] = 'ASC'; |
|
358
|
|
|
} |
|
359
|
|
|
} else { |
|
360
|
|
|
$values['_sort_order'] = 'ASC'; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
$values['_sort_by'] = \is_string($fieldDescription->getOption('sortable')) ? $fieldDescription->getOption('sortable') : $fieldDescription->getName(); |
|
364
|
|
|
|
|
365
|
|
|
return ['filter' => $values]; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
public function getPaginationParameters(DatagridInterface $datagrid, $page) |
|
369
|
|
|
{ |
|
370
|
|
|
$values = $datagrid->getValues(); |
|
371
|
|
|
|
|
372
|
|
|
if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) { |
|
373
|
|
|
$values['_sort_by'] = $values['_sort_by']->getName(); |
|
374
|
|
|
} |
|
375
|
|
|
$values['_page'] = $page; |
|
376
|
|
|
|
|
377
|
|
|
return ['filter' => $values]; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
public function getDefaultSortValues($class) |
|
381
|
|
|
{ |
|
382
|
|
|
return [ |
|
383
|
|
|
'_page' => 1, |
|
384
|
|
|
'_per_page' => 25, |
|
385
|
|
|
]; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
public function getDefaultPerPageOptions(string $class): array |
|
|
|
|
|
|
389
|
|
|
{ |
|
390
|
|
|
return [10, 25, 50, 100, 250]; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
public function modelTransform($class, $instance) |
|
394
|
|
|
{ |
|
395
|
|
|
return $instance; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
public function modelReverseTransform($class, array $array = []) |
|
399
|
|
|
{ |
|
400
|
|
|
$instance = $this->getModelInstance($class); |
|
401
|
|
|
$metadata = $this->getMetadata($class); |
|
402
|
|
|
|
|
403
|
|
|
foreach ($array as $name => $value) { |
|
404
|
|
|
$property = $this->getFieldName($metadata, $name); |
|
405
|
|
|
|
|
406
|
|
|
$this->propertyAccessor->setValue($instance, $property, $value); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
return $instance; |
|
|
|
|
|
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
public function getModelCollectionInstance($class) |
|
413
|
|
|
{ |
|
414
|
|
|
return new ArrayCollection(); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
public function collectionClear(&$collection) |
|
418
|
|
|
{ |
|
419
|
|
|
return $collection->clear(); |
|
|
|
|
|
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
public function collectionHasElement(&$collection, &$element) |
|
423
|
|
|
{ |
|
424
|
|
|
return $collection->contains($element); |
|
|
|
|
|
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
public function collectionAddElement(&$collection, &$element) |
|
428
|
|
|
{ |
|
429
|
|
|
return $collection->add($element); |
|
|
|
|
|
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
public function collectionRemoveElement(&$collection, &$element) |
|
433
|
|
|
{ |
|
434
|
|
|
return $collection->removeElement($element); |
|
|
|
|
|
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* NEXT_MAJOR: Remove this method. |
|
439
|
|
|
* |
|
440
|
|
|
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
|
441
|
|
|
* |
|
442
|
|
|
* @param string $property |
|
443
|
|
|
* |
|
444
|
|
|
* @return mixed |
|
445
|
|
|
*/ |
|
446
|
|
|
protected function camelize($property) |
|
447
|
|
|
{ |
|
448
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
449
|
|
|
'Method "%s()" is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.', |
|
450
|
|
|
__METHOD__ |
|
451
|
|
|
), E_USER_DEPRECATED); |
|
452
|
|
|
|
|
453
|
|
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* NEXT_MAJOR: Remove CommonClassMetadata and add ClassMetadata as type hint when dropping doctrine/mongodb-odm 1.3.x. |
|
458
|
|
|
* |
|
459
|
|
|
* @param ClassMetadata|CommonClassMetadata $metadata |
|
460
|
|
|
*/ |
|
461
|
|
|
private function getFieldName($metadata, string $name): string |
|
462
|
|
|
{ |
|
463
|
|
|
if (\array_key_exists($name, $metadata->fieldMappings)) { |
|
|
|
|
|
|
464
|
|
|
return $metadata->fieldMappings[$name]['fieldName']; |
|
|
|
|
|
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
if (\array_key_exists($name, $metadata->associationMappings)) { |
|
|
|
|
|
|
468
|
|
|
return $metadata->associationMappings[$name]['fieldName']; |
|
|
|
|
|
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
return $name; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
|
475
|
|
|
{ |
|
476
|
|
|
$values = $datagrid->getValues(); |
|
477
|
|
|
|
|
478
|
|
|
if (!isset($values['_sort_by']) || !$values['_sort_by'] instanceof FieldDescriptionInterface) { |
|
479
|
|
|
return false; |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
return $values['_sort_by']->getName() === $fieldDescription->getName() |
|
483
|
|
|
|| $values['_sort_by']->getName() === $fieldDescription->getOption('sortable'); |
|
484
|
|
|
} |
|
485
|
|
|
} |
|
486
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: