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\DoctrineORMAdminBundle\Admin; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\BaseFieldDescription; |
17
|
|
|
|
18
|
|
|
class FieldDescription extends BaseFieldDescription |
19
|
|
|
{ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->parentAssociationMappings = []; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setAssociationMapping($associationMapping) |
26
|
|
|
{ |
27
|
|
|
if (!\is_array($associationMapping)) { |
28
|
|
|
throw new \RuntimeException('The association mapping must be an array'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->associationMapping = $associationMapping; |
32
|
|
|
|
33
|
|
|
$this->type = $this->type ?: $associationMapping['type']; |
34
|
|
|
$this->mappingType = $this->mappingType ?: $associationMapping['type']; |
35
|
|
|
$this->fieldName = $associationMapping['fieldName']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* NEXT_MAJOR: Remove this method. |
40
|
|
|
*/ |
41
|
|
|
public function getTargetEntity() |
42
|
|
|
{ |
43
|
|
|
@trigger_error(sprintf( |
|
|
|
|
44
|
|
|
'Method %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.' |
45
|
|
|
.' Use %s::getTargetModel() instead.', |
46
|
|
|
__METHOD__, |
47
|
|
|
__CLASS__ |
48
|
|
|
), E_USER_DEPRECATED); |
49
|
|
|
|
50
|
|
|
return $this->getTargetModel(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getTargetModel(): ?string |
54
|
|
|
{ |
55
|
|
|
if ($this->associationMapping) { |
|
|
|
|
56
|
|
|
return $this->associationMapping['targetEntity']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setFieldMapping($fieldMapping) |
63
|
|
|
{ |
64
|
|
|
if (!\is_array($fieldMapping)) { |
65
|
|
|
throw new \RuntimeException('The field mapping must be an array'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->fieldMapping = $fieldMapping; |
69
|
|
|
|
70
|
|
|
$this->type = $this->type ?: $fieldMapping['type']; |
71
|
|
|
$this->mappingType = $this->mappingType ?: $fieldMapping['type']; |
72
|
|
|
$this->fieldName = $this->fieldName ?: $fieldMapping['fieldName']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function setParentAssociationMappings(array $parentAssociationMappings) |
76
|
|
|
{ |
77
|
|
|
foreach ($parentAssociationMappings as $parentAssociationMapping) { |
78
|
|
|
if (!\is_array($parentAssociationMapping)) { |
79
|
|
|
throw new \RuntimeException('An association mapping must be an array'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->parentAssociationMappings = $parentAssociationMappings; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isIdentifier() |
87
|
|
|
{ |
88
|
|
|
return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getValue($object) |
92
|
|
|
{ |
93
|
|
|
foreach ($this->parentAssociationMappings as $parentAssociationMapping) { |
94
|
|
|
$object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$fieldMapping = $this->getFieldMapping(); |
98
|
|
|
// Support embedded object for mapping |
99
|
|
|
// Ref: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/embeddables.html |
100
|
|
|
if (isset($fieldMapping['declaredField'])) { |
101
|
|
|
$parentFields = explode('.', $fieldMapping['declaredField']); |
102
|
|
|
foreach ($parentFields as $parentField) { |
103
|
|
|
$object = $this->getFieldValue($object, $parentField); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->getFieldValue($object, $this->fieldName); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: