|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface as Container; |
|
6
|
|
|
use Doctrine\Common\Persistence\Proxy; |
|
7
|
|
|
|
|
8
|
|
|
class EntityUtility |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
private $container; |
|
12
|
|
|
private $em; |
|
13
|
|
|
|
|
14
|
1 |
|
public function __construct(Container $container) |
|
15
|
|
|
{ |
|
16
|
1 |
|
$this->container = $container; |
|
17
|
1 |
|
$this->em = $this->container->get("doctrine")->getManager(); |
|
|
|
|
|
|
18
|
1 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function getTableFromEntity($entity) |
|
21
|
|
|
{ |
|
22
|
1 |
|
$metadata = $this->em->getClassMetadata($entity); |
|
|
|
|
|
|
23
|
1 |
|
$tablename = $metadata->table["name"]; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
1 |
|
return $tablename; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function entityHasJoinTables($entityclass) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$jointables = $this->getEntityJoinTables($entityclass); |
|
31
|
1 |
|
return count($jointables) > 0 ? true : false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function getJoinTableField($entityclass, $field) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
|
37
|
1 |
|
foreach ($joinfields as $joinfield) { |
|
38
|
1 |
|
if (count($joinfield) != 1) { |
|
39
|
|
|
return null; |
|
40
|
|
|
} |
|
41
|
1 |
|
$jointableentity = $this->getJoinTable($joinfield, $field); |
|
42
|
1 |
|
if ($jointableentity) { |
|
43
|
1 |
|
return $jointableentity; |
|
44
|
|
|
} |
|
45
|
1 |
|
} |
|
46
|
1 |
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function getJoinTableFieldProperty($entityclass, $field) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
|
52
|
1 |
|
foreach ($joinfields as $joinfield) { |
|
53
|
1 |
|
if (count($joinfield) != 1) { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
1 |
|
$joinfieldname = $this->getJoinFieldName($joinfield, $field); |
|
57
|
1 |
|
if ($joinfieldname) { |
|
58
|
1 |
|
return $joinfieldname; |
|
59
|
|
|
} |
|
60
|
1 |
|
} |
|
61
|
1 |
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function getEntityProperties($fieldname, $objrecord) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$parametri = array('str' => $fieldname, 'primamaiuscola' => true); |
|
|
|
|
|
|
67
|
1 |
|
$getfieldname = \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
68
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
69
|
1 |
|
$getfieldname = "has" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
70
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
71
|
1 |
|
$getfieldname = "is" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
72
|
1 |
|
if (!method_exists($objrecord, $getfieldname)) { |
|
73
|
1 |
|
$getfieldname = "get" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
74
|
1 |
|
} |
|
75
|
1 |
|
} |
|
76
|
1 |
|
} |
|
77
|
1 |
|
$setfieldname = "set" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
1 |
|
return array("get" => $getfieldname, "set" => $setfieldname); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function entityExists($className) |
|
83
|
|
|
{ |
|
84
|
|
|
|
|
85
|
1 |
|
if (is_object($className)) { |
|
86
|
|
|
$className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return !$this->em->getMetadataFactory()->isTransient($className); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function getEntityJoinTables($entityclass) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$jointables = array(); |
|
95
|
1 |
|
$metadata = $this->em->getClassMetadata($entityclass); |
|
|
|
|
|
|
96
|
1 |
|
$fielsassoc = $metadata->associationMappings; |
|
97
|
1 |
|
foreach ($fielsassoc as $tableassoc) { |
|
98
|
1 |
|
if ($tableassoc["inversedBy"]) { |
|
|
|
|
|
|
99
|
1 |
|
$jointables[$tableassoc["targetEntity"]] = array("entity" => $tableassoc); |
|
|
|
|
|
|
100
|
1 |
|
} |
|
101
|
1 |
|
} |
|
102
|
1 |
|
return $jointables; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
private function getJoinFieldName($joinfield, $field) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$joinFieldentity = $joinfield["entity"]; |
|
|
|
|
|
|
108
|
1 |
|
$joinColumns = $joinFieldentity["joinColumns"]; |
|
|
|
|
|
|
109
|
1 |
|
foreach ($joinColumns as $joinColumn) { |
|
110
|
1 |
|
if ($field === $joinColumn["name"]) { |
|
|
|
|
|
|
111
|
1 |
|
$joinFieldName = $joinFieldentity["fieldName"]; |
|
|
|
|
|
|
112
|
1 |
|
return $joinFieldName; |
|
113
|
|
|
} |
|
114
|
1 |
|
} |
|
115
|
1 |
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
private function getJoinTable($joinfield, $field) |
|
119
|
|
|
{ |
|
120
|
1 |
|
$joinTableEntity = $joinfield["entity"]; |
|
|
|
|
|
|
121
|
1 |
|
$joinColumns = $joinTableEntity["joinColumns"]; |
|
|
|
|
|
|
122
|
1 |
|
foreach ($joinColumns as $joinColumn) { |
|
123
|
1 |
|
if ($field === $joinColumn["name"]) { |
|
|
|
|
|
|
124
|
1 |
|
return $joinTableEntity["targetEntity"]; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
1 |
|
} |
|
127
|
1 |
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
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.