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