-
Notifications
You must be signed in to change notification settings - Fork 82
/
UpdateClassNamesQuery.php
78 lines (63 loc) · 1.82 KB
/
UpdateClassNamesQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Oro\Bundle\FrontendBundle\Migration;
use Doctrine\DBAL\Types\Type;
use Psr\Log\LoggerInterface;
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
use Oro\Bundle\EntityConfigBundle\Config\Id\FieldConfigId;
class UpdateClassNamesQuery extends ParametrizedMigrationQuery
{
/**
* @var string
*/
private $table;
/**
* @var string
*/
private $field;
/**
* @param string $table
* @param string $field
*/
public function __construct($table, $field)
{
$this->table = $table;
$this->field = $field;
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
$logger = new ArrayLogger();
$this->processQueries($logger, true);
return $logger->getMessages();
}
/**
* {@inheritdoc}
*/
public function execute(LoggerInterface $logger)
{
$this->processQueries($logger);
}
/**
* @param LoggerInterface $logger
* @param bool $dryRun
*/
protected function processQueries(LoggerInterface $logger, $dryRun = false)
{
$table = $this->table;
$field = $this->field;
$statement = $this->connection->query("SELECT id, $field FROM $table WHERE $field LIKE 'OroB2B%'");
while ($entity = $statement->fetch(\PDO::FETCH_ASSOC)) {
$originalClass = $entity[$field];
$class = preg_replace('/^OroB2B/', 'Oro', $originalClass, 1);
$query = "UPDATE $table SET $field = ? WHERE id = ?";
$parameters = [$class, $entity['id']];
$this->logQuery($logger, $query, $parameters);
if (!$dryRun) {
$this->connection->executeUpdate($query, $parameters);
}
}
}
}