Completed
Pull Request — master (#142)
by Ihor
15:10
created

Version20170402155012::setVenueSectorSlugs()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 0
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Application\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
8
/**
9
 * Auto-generated Migration: Please modify to your needs!
10
 */
11
class Version20170402155012 extends AbstractMigration
12
{
13
    protected $venue_sector_slugs = [
14
        'Партер' => 'parterre',
15
        'Балкон' => 'balcony',
16
        'Лоджия Левая' => 'loggia-left',
17
    	'Лоджия Правая' => 'loggia-right',
18
    ];
19
20
    /**
21
     * @param Schema $schema
22
     */
23
    public function up(Schema $schema)
24
    {
25
        // this up() migration is auto-generated, please modify it to your needs
26
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
27
28
        $this->addSql('ALTER TABLE `venue_sector` ADD `slug` VARCHAR(255) NOT NULL');
29
    }
30
31
    public function postUp(Schema $schema)
32
    {
33
        $this->setVenueSectorSlugs();
34
    }
35
36
    /**
37
     * @param Schema $schema
38
     */
39
    public function down(Schema $schema)
40
    {
41
        // this down() migration is auto-generated, please modify it to your needs
42
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
43
44
        $this->addSql('ALTER TABLE `venue_sector` DROP slug');
45
    }
46
47
    private function setVenueSectorSlugs():void
48
    {
49
        $query = "SELECT * FROM `venue_sector`";
50
        $stmt = $this->connection->prepare($query);
51
        $stmt->execute();
52
53
        while ($row = $stmt->fetch()) {
54
            if (empty($row['title']) || empty($this->venue_sector_slugs[$row['title']])) {
55
                continue;
56
            }
57
            $this->connection->update(
58
                'venue_sector',
59
                ['slug' => $this->venue_sector_slugs[$row['title']]],
60
                ['id' => $row['id']]
61
            );
62
        }
63
    }
64
}
65