|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Utils\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Proxy; |
|
6
|
|
|
|
|
7
|
|
|
class EntityUtils |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
private $em; |
|
11
|
|
|
|
|
12
|
9 |
|
public function __construct($em) |
|
13
|
|
|
{ |
|
14
|
9 |
|
$this->em = $em; |
|
15
|
9 |
|
} |
|
16
|
|
|
|
|
17
|
6 |
|
public function getClassNameToShortcutNotations($entity) |
|
18
|
|
|
{ |
|
19
|
6 |
|
$cleanClassName = str_replace('\\Entity', '\:', $entity); |
|
20
|
6 |
|
$parts = explode('\\', $cleanClassName); |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
6 |
|
return implode('', $parts); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
8 |
|
public function getEntityColumns($entity) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$infocolonne = $this->em->getMetadataFactory()->getMetadataFor($entity); |
|
|
|
|
|
|
28
|
8 |
|
$colonne = array(); |
|
|
|
|
|
|
29
|
8 |
|
$fieldMappings = $infocolonne->fieldMappings; |
|
30
|
8 |
|
foreach ($fieldMappings as $colonna) { |
|
31
|
8 |
|
$colonne[$colonna['fieldName']] = $colonna; |
|
|
|
|
|
|
32
|
8 |
|
$colonne[$colonna['fieldName']]["entityClass"] = $entity; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
8 |
|
$joinTables = $this->getEntityJoinTables($entity); |
|
35
|
8 |
|
foreach ($joinTables as $entityjoin => $entityproperties) { |
|
36
|
2 |
|
$key = $entityproperties["entity"]["fieldName"]; |
|
|
|
|
|
|
37
|
2 |
|
$colonne[$key]["fieldName"] = $key; |
|
|
|
|
|
|
38
|
2 |
|
$colonne[$key]["columnName"] = $key; |
|
|
|
|
|
|
39
|
2 |
|
$colonne[$key]["entityClass"] = $entityjoin; |
|
|
|
|
|
|
40
|
2 |
|
$colonne[$key]["sourceEntityClass"] = $entity; |
|
|
|
|
|
|
41
|
2 |
|
$colonne[$key]["association"] = true; |
|
|
|
|
|
|
42
|
2 |
|
$colonne[$key]["associationtable"] = $entityproperties["entity"]; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
8 |
|
return $colonne; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
6 |
|
public function getTableFromEntity($entity) |
|
48
|
|
|
{ |
|
49
|
6 |
|
$metadata = $this->em->getClassMetadata($entity); |
|
|
|
|
|
|
50
|
6 |
|
$tablename = $metadata->getTablename(); |
|
51
|
|
|
|
|
52
|
6 |
|
return $tablename; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function entityHasJoinTables($entityclass) |
|
56
|
|
|
{ |
|
57
|
|
|
$jointables = $this->getEntityJoinTables($entityclass); |
|
58
|
|
|
return count($jointables) > 0 ? true : false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function getJoinTableField($entityclass, $field) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
|
64
|
1 |
|
foreach ($joinfields as $joinfield) { |
|
65
|
1 |
|
if (count($joinfield) != 1) { |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
1 |
|
$jointableentity = $this->getJoinTable($joinfield, $field); |
|
69
|
1 |
|
if ($jointableentity) { |
|
70
|
1 |
|
return $jointableentity; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
1 |
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function getJoinTableFieldProperty($entityclass, $field) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
|
79
|
1 |
|
foreach ($joinfields as $joinfield) { |
|
80
|
1 |
|
if (count($joinfield) != 1) { |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
1 |
|
$joinfieldname = $this->getJoinFieldName($joinfield, $field); |
|
84
|
1 |
|
if ($joinfieldname) { |
|
85
|
1 |
|
return $joinfieldname; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
1 |
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function getEntityProperties($fieldname, $objrecord) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$parametri = array('str' => $fieldname, 'primamaiuscola' => true); |
|
|
|
|
|
|
94
|
1 |
|
$getfieldname = \Fi\CoreBundle\Utils\String\StringUtils::toCamelCase($parametri); |
|
95
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
96
|
1 |
|
$getfieldname = "has" . \Fi\CoreBundle\Utils\String\StringUtils::toCamelCase($parametri); |
|
|
|
|
|
|
97
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
98
|
1 |
|
$getfieldname = "is" . \Fi\CoreBundle\Utils\String\StringUtils::toCamelCase($parametri); |
|
|
|
|
|
|
99
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
100
|
1 |
|
$getfieldname = "get" . \Fi\CoreBundle\Utils\String\StringUtils::toCamelCase($parametri); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
1 |
|
$setfieldname = "set" . \Fi\CoreBundle\Utils\String\StringUtils::toCamelCase($parametri); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
1 |
|
return array("get" => $getfieldname, "set" => $setfieldname); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function entityExists($className) |
|
110
|
|
|
{ |
|
111
|
|
|
|
|
112
|
1 |
|
if (is_object($className)) { |
|
113
|
|
|
$className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
return !$this->em->getMetadataFactory()->isTransient($className); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
9 |
|
public function getEntityJoinTables($entityclass) |
|
120
|
|
|
{ |
|
121
|
9 |
|
$jointables = array(); |
|
122
|
9 |
|
$metadata = $this->em->getClassMetadata($entityclass); |
|
|
|
|
|
|
123
|
9 |
|
$fielsassoc = $metadata->associationMappings; |
|
124
|
9 |
|
foreach ($fielsassoc as $tableassoc) { |
|
125
|
7 |
|
if ($tableassoc["inversedBy"]) { |
|
|
|
|
|
|
126
|
7 |
|
$jointables[$tableassoc["targetEntity"]] = array("entity" => $tableassoc); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
9 |
|
return $jointables; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
private function getJoinFieldName($joinfield, $field) |
|
133
|
|
|
{ |
|
134
|
1 |
|
$joinFieldentity = $joinfield["entity"]; |
|
|
|
|
|
|
135
|
1 |
|
$joinColumns = $joinFieldentity["joinColumns"]; |
|
|
|
|
|
|
136
|
1 |
|
foreach ($joinColumns as $joinColumn) { |
|
137
|
1 |
|
if ($field === $joinColumn["name"]) { |
|
|
|
|
|
|
138
|
1 |
|
$joinFieldName = $joinFieldentity["fieldName"]; |
|
|
|
|
|
|
139
|
1 |
|
return $joinFieldName; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
1 |
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
private function getJoinTable($joinfield, $field) |
|
146
|
|
|
{ |
|
147
|
1 |
|
$joinTableEntity = $joinfield["entity"]; |
|
|
|
|
|
|
148
|
1 |
|
$joinColumns = $joinTableEntity["joinColumns"]; |
|
|
|
|
|
|
149
|
1 |
|
foreach ($joinColumns as $joinColumn) { |
|
150
|
1 |
|
if ($field === $joinColumn["name"]) { |
|
|
|
|
|
|
151
|
1 |
|
return $joinTableEntity["targetEntity"]; |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
1 |
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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.