|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
8
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
9
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface as Container; |
|
11
|
|
|
|
|
12
|
|
|
class DatabaseUtility |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $container; |
|
16
|
|
|
/* @var $em EntityManager */ |
|
|
|
|
|
|
17
|
|
|
private $em; |
|
18
|
|
|
|
|
19
|
2 |
|
public function __construct(Container $container) |
|
20
|
|
|
{ |
|
21
|
2 |
|
$this->container = $container; |
|
22
|
2 |
|
$this->em = $this->container->get("doctrine")->getManager(); |
|
23
|
2 |
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
public function getFieldType($entity, $field) |
|
26
|
|
|
{ |
|
27
|
2 |
|
$metadata = $this->em->getClassMetadata(get_class($entity)); |
|
28
|
2 |
|
$fieldMetadata = $metadata->fieldMappings[$field]; |
|
29
|
|
|
|
|
30
|
2 |
|
$fieldType = $fieldMetadata['type']; |
|
31
|
2 |
|
return $fieldType; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$fieldtype = $this->getFieldType(new $entity(), $fieldname); |
|
37
|
1 |
|
if ($fieldtype === 'boolean') { |
|
38
|
1 |
|
return $oldvalue !== $newvalue; |
|
39
|
|
|
} |
|
40
|
1 |
|
if ($fieldtype === 'datetime' || $fieldtype === 'date') { |
|
41
|
1 |
|
return $this->isDateChanged($oldvalue, $newvalue); |
|
42
|
|
|
} |
|
43
|
1 |
|
if (is_array($oldvalue)) { |
|
44
|
1 |
|
return $this->isArrayChanged($oldvalue, $newvalue); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
return ($oldvalue !== $newvalue); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function isDateChanged($oldvalue, $newvalue) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$datenewvalue = new DateTime(); |
|
53
|
1 |
|
$datenewvalue->setTimestamp($newvalue); |
|
54
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
|
55
|
1 |
|
if ($twoboth) { |
|
56
|
1 |
|
return false; |
|
57
|
|
|
} |
|
58
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
|
|
|
|
|
59
|
1 |
|
if ($onlyonenull) { |
|
60
|
1 |
|
return true; |
|
61
|
|
|
} |
|
62
|
1 |
|
$changed = ($oldvalue != $datenewvalue); |
|
63
|
1 |
|
return $changed; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function isArrayChanged($oldvalue, $newvalue) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
|
69
|
1 |
|
if ($twoboth) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
|
|
|
|
|
73
|
1 |
|
if ($onlyonenull) { |
|
74
|
1 |
|
return true; |
|
75
|
|
|
} |
|
76
|
1 |
|
$numdiff = array_diff($oldvalue, $newvalue); |
|
77
|
1 |
|
return count($numdiff) > 0; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function truncateTable($entityclass, $cascade = false) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$cmd = $this->em->getClassMetadata($entityclass); |
|
83
|
1 |
|
$connection = $this->em->getConnection(); |
|
84
|
1 |
|
$dbPlatform = $connection->getDatabasePlatform(); |
|
85
|
1 |
|
$dbtype = $connection->getDriver()->getDatabasePlatform()->getName(); |
|
86
|
1 |
|
$cascademysql = $cascade && $dbtype === 'mysql'; |
|
87
|
1 |
|
if ($cascademysql) { |
|
88
|
|
|
$connection->query('SET FOREIGN_KEY_CHECKS=0'); |
|
89
|
|
|
} |
|
90
|
1 |
|
$q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade); |
|
91
|
1 |
|
$connection->executeUpdate($q); |
|
92
|
1 |
|
if ($cascademysql) { |
|
93
|
|
|
$connection->query('SET FOREIGN_KEY_CHECKS=1'); |
|
94
|
|
|
} |
|
95
|
1 |
|
$this->em->clear(); |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
public function isSchemaChanged() |
|
99
|
|
|
{ |
|
100
|
2 |
|
$kernel = $this->container->get("kernel"); |
|
101
|
2 |
|
$application = new Application($kernel); |
|
|
|
|
|
|
102
|
2 |
|
$application->setAutoExit(false); |
|
103
|
|
|
|
|
104
|
2 |
|
$input = new ArrayInput(array( |
|
105
|
2 |
|
'command' => 'doctrine:schema:update', |
|
106
|
|
|
'--dump-sql' => true, |
|
107
|
|
|
'--no-debug' => true, |
|
108
|
2 |
|
'--env' => $kernel->getEnvironment(), |
|
109
|
|
|
)); |
|
110
|
|
|
|
|
111
|
|
|
// You can use NullOutput() if you don't need the output |
|
112
|
2 |
|
$output = new BufferedOutput(); |
|
113
|
2 |
|
$application->run($input, $output); |
|
114
|
|
|
|
|
115
|
|
|
// return the output, don't use if you used NullOutput() |
|
116
|
2 |
|
$content = $output->fetch(); |
|
117
|
2 |
|
$changed = strpos($content, 'Nothing to update'); |
|
118
|
2 |
|
return ($changed!==0); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|