|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface as Container; |
|
6
|
|
|
|
|
7
|
|
|
class DatabaseUtility |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
private $container; |
|
11
|
|
|
private $em; |
|
12
|
|
|
|
|
13
|
1 |
|
public function __construct(Container $container) |
|
14
|
|
|
{ |
|
15
|
1 |
|
$this->container = $container; |
|
16
|
1 |
|
$this->em = $this->container->get("doctrine")->getManager(); |
|
|
|
|
|
|
17
|
1 |
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
public function getFieldType($entity, $field) |
|
20
|
|
|
{ |
|
21
|
1 |
|
$metadata = $this->em->getClassMetadata(get_class($entity)); |
|
|
|
|
|
|
22
|
1 |
|
$fieldMetadata = $metadata->fieldMappings[$field]; |
|
23
|
|
|
|
|
24
|
1 |
|
$fieldType = $fieldMetadata['type']; |
|
25
|
1 |
|
return $fieldType; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function getEntityProperties($fieldname, $objrecord) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$parametri = array('str' => $fieldname, 'primamaiuscola' => true); |
|
|
|
|
|
|
31
|
1 |
|
$getfieldname = \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
32
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
33
|
1 |
|
$getfieldname = "has" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
34
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
35
|
1 |
|
$getfieldname = "is" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
36
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
37
|
1 |
|
$getfieldname = "get" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
38
|
1 |
|
} |
|
39
|
1 |
|
} |
|
40
|
1 |
|
} |
|
41
|
1 |
|
$setfieldname = "set" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
1 |
|
return array("get" => $getfieldname, "set" => $setfieldname); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$fieldtype = $this->getFieldType(new $entity(), $fieldname); |
|
49
|
1 |
|
if ($fieldtype === 'datetime') { |
|
50
|
1 |
|
return $this->isDateChanged($oldvalue, $newvalue); |
|
51
|
|
|
} |
|
52
|
1 |
|
if (is_array($oldvalue)) { |
|
53
|
1 |
|
return $this->isArrayChanged($oldvalue, $newvalue); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return ($oldvalue !== $newvalue); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function isDateChanged($oldvalue, $newvalue) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$datenewvalue = new \DateTime(); |
|
62
|
1 |
|
$datenewvalue->setTimestamp($newvalue); |
|
63
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
|
64
|
1 |
|
if ($twoboth) { |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
68
|
1 |
|
if ($onlyonenull) { |
|
69
|
1 |
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
$changed = ($oldvalue != $datenewvalue); |
|
72
|
|
|
return $changed; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function isArrayChanged($oldvalue, $newvalue) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
|
78
|
1 |
|
if ($twoboth) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
82
|
1 |
|
if ($onlyonenull) { |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
1 |
|
$numdiff = array_diff($oldvalue, $newvalue); |
|
86
|
1 |
|
return count($numdiff) > 0; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
public function entityExists($className) |
|
90
|
|
|
{ |
|
91
|
|
|
|
|
92
|
1 |
|
if (is_object($className)) { |
|
93
|
|
|
$className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return !$this->em->getMetadataFactory()->isTransient($className); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function getEntityJoinTables($entityclass) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$jointables = array(); |
|
102
|
1 |
|
$metadata = $this->em->getClassMetadata($entityclass); |
|
|
|
|
|
|
103
|
1 |
|
$fielsassoc = $metadata->associationMappings; |
|
104
|
1 |
|
foreach ($fielsassoc as $tableassoc) { |
|
105
|
1 |
|
if ($tableassoc["inversedBy"]) { |
|
|
|
|
|
|
106
|
1 |
|
$jointables[$tableassoc["targetEntity"]] = array("entity" => $tableassoc); |
|
|
|
|
|
|
107
|
1 |
|
} |
|
108
|
1 |
|
} |
|
109
|
1 |
|
return $jointables; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
public function getJoinTableField($entityclass, $field) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
|
115
|
1 |
|
foreach ($joinfields as $joinfield) { |
|
116
|
1 |
|
if (count($joinfield) != 1) { |
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
1 |
|
$jointableentity = $this->getJoinTable($joinfield, $field); |
|
120
|
1 |
|
if ($jointableentity) { |
|
121
|
1 |
|
return $jointableentity; |
|
122
|
|
|
} |
|
123
|
1 |
|
} |
|
124
|
1 |
|
return null; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
public function getJoinTableFieldProperty($entityclass, $field) |
|
128
|
|
|
{ |
|
129
|
1 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
|
130
|
1 |
|
foreach ($joinfields as $joinfield) { |
|
131
|
1 |
|
if (count($joinfield) != 1) { |
|
132
|
|
|
return null; |
|
133
|
|
|
} |
|
134
|
1 |
|
$joinfieldname = $this->getJoinFieldName($joinfield, $field); |
|
135
|
1 |
|
if ($joinfieldname) { |
|
136
|
1 |
|
return $joinfieldname; |
|
137
|
|
|
} |
|
138
|
1 |
|
} |
|
139
|
1 |
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
public function entityHasJoinTables($entityclass) |
|
143
|
|
|
{ |
|
144
|
1 |
|
$jointables = $this->getEntityJoinTables($entityclass); |
|
145
|
1 |
|
return count($jointables) > 0 ? true : false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
private function getJoinFieldName($joinfield, $field) |
|
149
|
|
|
{ |
|
150
|
1 |
|
$joinFieldentity = $joinfield["entity"]; |
|
|
|
|
|
|
151
|
1 |
|
$joinColumns = $joinFieldentity["joinColumns"]; |
|
|
|
|
|
|
152
|
1 |
|
foreach ($joinColumns as $joinColumn) { |
|
153
|
1 |
|
if ($field === $joinColumn["name"]) { |
|
|
|
|
|
|
154
|
1 |
|
$joinFieldName = $joinFieldentity["fieldName"]; |
|
|
|
|
|
|
155
|
1 |
|
return $joinFieldName; |
|
156
|
|
|
} |
|
157
|
1 |
|
} |
|
158
|
1 |
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
private function getJoinTable($joinfield, $field) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$joinTableEntity = $joinfield["entity"]; |
|
|
|
|
|
|
164
|
1 |
|
$joinColumns = $joinTableEntity["joinColumns"]; |
|
|
|
|
|
|
165
|
1 |
|
foreach ($joinColumns as $joinColumn) { |
|
166
|
1 |
|
if ($field === $joinColumn["name"]) { |
|
|
|
|
|
|
167
|
1 |
|
return $joinTableEntity["targetEntity"]; |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
1 |
|
} |
|
170
|
1 |
|
return null; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getTableFromEntity($entity) |
|
174
|
|
|
{ |
|
175
|
|
|
$metadata = $this->em->getClassMetadata($entity); |
|
|
|
|
|
|
176
|
|
|
$tablename = $metadata->table["name"]; |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
return $tablename; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function truncateTable($table, $cascade = false) |
|
182
|
|
|
{ |
|
183
|
|
|
$connection = $this->em->getConnection(); |
|
184
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
$connection->executeUpdate($platform->getTruncateTableSQL($table, $cascade)); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.