|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils; |
|
6
|
|
|
|
|
7
|
|
|
trait ConfiguratorimportUpdateTrait |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
1 |
|
private function executeUpdate($entityclass, $record, $objrecord) |
|
11
|
|
|
{ |
|
12
|
1 |
|
unset($record['id']); |
|
13
|
1 |
|
foreach ($record as $key => $value) { |
|
14
|
1 |
|
if ('discr' == $key) { |
|
15
|
1 |
|
continue; |
|
16
|
|
|
} |
|
17
|
1 |
|
$propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord); |
|
18
|
1 |
|
$getfieldname = $propertyEntity['get']; |
|
|
|
|
|
|
19
|
1 |
|
$cambiato = $this->dbutility->isRecordChanged($entityclass, $key, $objrecord->$getfieldname(), $value); |
|
|
|
|
|
|
20
|
1 |
|
if (!$cambiato) { |
|
21
|
1 |
|
if ($this->verboso) { |
|
22
|
1 |
|
$msginfo = '<info>' . $entityclass . ' con id ' . $objrecord->getId() |
|
23
|
1 |
|
. ' per campo ' . $key . ' non modificato perchè già ' |
|
24
|
1 |
|
. var_export($value, true) . '</info>'; |
|
25
|
1 |
|
$this->output->writeln($msginfo); |
|
26
|
|
|
} |
|
27
|
|
|
} else { |
|
28
|
|
|
try { |
|
29
|
1 |
|
$this->update($objrecord, $key, $value, $entityclass); |
|
30
|
|
|
} catch (\Exception $exc) { |
|
31
|
|
|
$msgerr = '<error>Modifica ' . $entityclass . ' con id ' . $objrecord->getId() |
|
32
|
|
|
. ' per campo ' . $key . ', ERRORE: ' . $exc->getMessage() |
|
33
|
|
|
. ' alla riga ' . $exc->getLine() . '</error>'; |
|
34
|
|
|
$this->output->writeln($msgerr); |
|
35
|
|
|
//dump($exc); |
|
36
|
|
|
return 1; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
1 |
|
$this->em->persist($objrecord); |
|
41
|
1 |
|
$this->em->flush(); |
|
42
|
|
|
|
|
43
|
1 |
|
return 0; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
private function update(&$objrecord, $key, $value, $entityclass) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
|
|
|
|
|
|
49
|
1 |
|
$propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord); |
|
50
|
1 |
|
$getfieldname = $propertyEntity['get']; |
|
|
|
|
|
|
51
|
1 |
|
$setfieldname = $propertyEntity['set']; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
1 |
|
if ('boolean' === $fieldtype) { |
|
54
|
|
|
$newval = FieldTypeUtils::getBooleanValue($value); |
|
55
|
|
|
|
|
56
|
|
|
$msgok = '<info>Modifica ' . $entityclass . ' con id ' . $objrecord->getId() |
|
57
|
|
|
. ' per campo ' . $key . ' cambio valore da ' |
|
58
|
|
|
. var_export($objrecord->$getfieldname(), true) |
|
59
|
|
|
. ' a ' . var_export($newval, true) . ' in formato Boolean</info>'; |
|
60
|
|
|
$this->output->writeln($msgok); |
|
61
|
|
|
$objrecord->$setfieldname($newval); |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
//Si prende in considerazione solo il null del boolean, gli altri non si toccano |
|
65
|
1 |
|
if (!$value) { |
|
66
|
1 |
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
if ('datetime' === $fieldtype || 'date' === $fieldtype) { |
|
70
|
1 |
|
$date = FieldTypeUtils::getDateTimeValueFromTimestamp($value); |
|
|
|
|
|
|
71
|
1 |
|
$msgok = '<info>Modifica ' . $entityclass . ' con id ' . $objrecord->getId() |
|
72
|
1 |
|
. ' per campo ' . $key . ' cambio valore da ' |
|
73
|
|
|
//. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)" |
|
|
|
|
|
|
74
|
1 |
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format('Y-m-d H:i:s') : 'NULL') |
|
75
|
1 |
|
. ' a ' . $date->format('Y-m-d H:i:s') . ' in formato DateTime</info>'; |
|
76
|
1 |
|
$this->output->writeln($msgok); |
|
77
|
1 |
|
$objrecord->$setfieldname($date); |
|
78
|
1 |
|
return true; |
|
79
|
|
|
} |
|
80
|
1 |
|
if (is_array($value)) { |
|
81
|
1 |
|
$msgarray = '<info>Modifica ' . $entityclass . ' con id ' . $objrecord->getId() |
|
82
|
1 |
|
. ' per campo ' . $key . ' cambio valore da ' |
|
83
|
1 |
|
. json_encode($objrecord->$getfieldname()) . ' a ' |
|
84
|
1 |
|
. json_encode($value) . ' in formato array' . '</info>'; |
|
85
|
1 |
|
$this->output->writeln($msgarray); |
|
86
|
1 |
|
$objrecord->$setfieldname($value); |
|
87
|
1 |
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
$joincolumn = $this->entityutility->getJoinTableField($entityclass, $key); |
|
|
|
|
|
|
91
|
1 |
|
$joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key); |
|
92
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
|
93
|
1 |
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
|
94
|
1 |
|
$msgok = '<info>Modifica ' . $entityclass . ' con id ' . $objrecord->getId() |
|
|
|
|
|
|
95
|
1 |
|
. ' per campo ' . $key . ' cambio valore da ' . print_r($objrecord->$getfieldname(), true) |
|
96
|
1 |
|
. ' a ' . print_r($value, true) . ' tramite entity find</info>'; |
|
97
|
1 |
|
$this->output->writeln($msgok); |
|
98
|
1 |
|
$joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
|
|
99
|
1 |
|
$setfieldname = $joinobj['set']; |
|
100
|
1 |
|
$objrecord->$setfieldname($joincolumnobj); |
|
101
|
1 |
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
$msgok = '<info>Modifica ' . $entityclass . ' con id ' . $objrecord->getId() |
|
105
|
1 |
|
. ' per campo ' . $key . ' cambio valore da ' . print_r($objrecord->$getfieldname(), true) |
|
106
|
1 |
|
. ' a ' . print_r($value, true) . '</info>'; |
|
107
|
1 |
|
$this->output->writeln($msgok); |
|
108
|
1 |
|
$objrecord->$setfieldname($value); |
|
109
|
1 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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.