|
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 isRecordChanged($entity, $fieldname, $oldvalue, $newvalue) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$fieldtype = $this->getFieldType(new $entity(), $fieldname); |
|
31
|
1 |
|
if ($fieldtype === 'datetime') { |
|
32
|
1 |
|
return $this->isDateChanged($oldvalue, $newvalue); |
|
33
|
|
|
} |
|
34
|
1 |
|
if (is_array($oldvalue)) { |
|
35
|
1 |
|
return $this->isArrayChanged($oldvalue, $newvalue); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
return ($oldvalue !== $newvalue); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function isDateChanged($oldvalue, $newvalue) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$datenewvalue = new \DateTime(); |
|
44
|
1 |
|
$datenewvalue->setTimestamp($newvalue); |
|
45
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
|
46
|
1 |
|
if ($twoboth) { |
|
47
|
1 |
|
return false; |
|
48
|
|
|
} |
|
49
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
50
|
1 |
|
if ($onlyonenull) { |
|
51
|
1 |
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
$changed = ($oldvalue != $datenewvalue); |
|
54
|
|
|
return $changed; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function isArrayChanged($oldvalue, $newvalue) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
|
60
|
1 |
|
if ($twoboth) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
64
|
1 |
|
if ($onlyonenull) { |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
1 |
|
$numdiff = array_diff($oldvalue, $newvalue); |
|
68
|
1 |
|
return count($numdiff) > 0; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function truncateTable($entityclass, $cascade = false) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$cmd = $this->em->getClassMetadata($entityclass); |
|
|
|
|
|
|
74
|
1 |
|
$connection = $this->em->getConnection(); |
|
|
|
|
|
|
75
|
1 |
|
$dbPlatform = $connection->getDatabasePlatform(); |
|
|
|
|
|
|
76
|
1 |
|
$dbtype = $connection->getDriver()->getDatabasePlatform()->getName(); |
|
|
|
|
|
|
77
|
1 |
|
$cascademysql = $cascade && $dbtype === 'mysql'; |
|
78
|
1 |
|
if ($cascademysql) { |
|
79
|
|
|
$connection->query('SET FOREIGN_KEY_CHECKS=0'); |
|
80
|
|
|
} |
|
81
|
1 |
|
$q = $dbPlatform->getTruncateTableSql($cmd->getTableName()); |
|
82
|
1 |
|
$connection->executeUpdate($q); |
|
83
|
1 |
|
if ($cascademysql) { |
|
84
|
|
|
$connection->query('SET FOREIGN_KEY_CHECKS=1'); |
|
85
|
|
|
} |
|
86
|
1 |
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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.