|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\DoctrineMigrations; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Migrations\AbstractMigration; |
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
17
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Genre; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Auto-generated Migration: Please modify to your needs! |
|
21
|
|
|
*/ |
|
22
|
|
|
class Version20140407162508_RenameItemGenres extends AbstractMigration implements ContainerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $em; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Rename genres. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $rename = [ |
|
35
|
|
|
'Mysticism' => 'Mystery', |
|
36
|
|
|
'Ettie' => 'Ecchi', |
|
37
|
|
|
'For children' => ['Kids', 'Детское'], |
|
38
|
|
|
'Meho' => 'Mecha', |
|
39
|
|
|
'Musical' => ['Music', 'Музыка'], |
|
40
|
|
|
'Samurai action' => ['Samurai', 'Самураи'], |
|
41
|
|
|
'Senen' => 'Shounen', |
|
42
|
|
|
'Senen-ai' => 'Shounen-ai', |
|
43
|
|
|
'Psychology' => ['Psychological', 'Психологическое'], |
|
44
|
|
|
'Fantastic' => 'Sci-fi', |
|
45
|
|
|
'Everyday' => 'Slice of life', |
|
46
|
|
|
'Vampires' => 'Vampire', |
|
47
|
|
|
'Urey' => 'Yuri', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Restore genres. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $restore = [ |
|
56
|
|
|
'Mystery' => 'Mysticism', |
|
57
|
|
|
'Ecchi' => 'Ettie', |
|
58
|
|
|
'Kids' => ['For children', 'Для детей'], |
|
59
|
|
|
'Mecha' => 'Meho', |
|
60
|
|
|
'Music' => ['Musical', 'Музыкальный'], |
|
61
|
|
|
'Samurai' => ['Samurai action', 'Самурайский боевик'], |
|
62
|
|
|
'Shounen' => 'Senen', |
|
63
|
|
|
'Shounen-ai' => 'Senen-ai', |
|
64
|
|
|
'Psychological' => ['Psychology', 'Психология'], |
|
65
|
|
|
'Sci-fi' => 'Fantastic', |
|
66
|
|
|
'Slice of life' => 'Everyday', |
|
67
|
|
|
'Vampire' => 'Vampires', |
|
68
|
|
|
'Yuri' => 'Urey', |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set container. |
|
73
|
|
|
* |
|
74
|
|
|
* @param ContainerInterface $container |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setContainer(ContainerInterface $container = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->em = $container->get('doctrine.orm.entity_manager'); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Schema $schema |
|
83
|
|
|
*/ |
|
84
|
|
|
public function up(Schema $schema) |
|
85
|
|
|
{ |
|
86
|
|
|
$rep = $this->em->getRepository('AnimeDbCatalogBundle:Genre'); |
|
87
|
|
|
|
|
88
|
|
|
/* @var $genre Genre */ |
|
89
|
|
View Code Duplication |
foreach ($this->rename as $from => $to) { |
|
|
|
|
|
|
90
|
|
|
$genre = $rep->findOneBy(['name' => $from]); |
|
91
|
|
|
if (is_array($to)) { |
|
92
|
|
|
$genre->setName($to[1])->setTranslatableLocale('ru'); |
|
93
|
|
|
$this->em->persist($genre); |
|
94
|
|
|
$this->em->flush($genre); |
|
|
|
|
|
|
95
|
|
|
$to = $to[0]; |
|
96
|
|
|
} |
|
97
|
|
|
$genre->setName($to)->setTranslatableLocale('en'); |
|
98
|
|
|
$this->em->persist($genre); |
|
99
|
|
|
} |
|
100
|
|
|
// remove |
|
101
|
|
|
$genre = $rep->findOneBy(['name' => 'Mystery play']); |
|
102
|
|
|
$this->em->remove($genre); |
|
103
|
|
|
|
|
104
|
|
|
// rename russian |
|
105
|
|
|
$genre = $rep->findOneBy(['name' => 'History']); |
|
106
|
|
|
$genre->setName('Исторический')->setTranslatableLocale('ru'); |
|
107
|
|
|
$this->em->persist($genre); |
|
108
|
|
|
|
|
109
|
|
|
$genre = $rep->findOneBy(['name' => 'War']); |
|
110
|
|
|
$genre->setName('Военное')->setTranslatableLocale('ru'); |
|
111
|
|
|
$this->em->persist($genre); |
|
112
|
|
|
|
|
113
|
|
|
$this->em->flush(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Schema $schema |
|
118
|
|
|
*/ |
|
119
|
|
|
public function down(Schema $schema) |
|
120
|
|
|
{ |
|
121
|
|
|
$rep = $this->em->getRepository('AnimeDbCatalogBundle:Genre'); |
|
122
|
|
|
|
|
123
|
|
|
/* @var $genre Genre */ |
|
124
|
|
View Code Duplication |
foreach ($this->restore as $from => $to) { |
|
|
|
|
|
|
125
|
|
|
$genre = $rep->findOneBy(['name' => $from]); |
|
126
|
|
|
if (is_array($to)) { |
|
127
|
|
|
$genre->setName($to[1])->setTranslatableLocale('ru'); |
|
128
|
|
|
$this->em->persist($genre); |
|
129
|
|
|
$this->em->flush($genre); |
|
|
|
|
|
|
130
|
|
|
$to = $to[0]; |
|
131
|
|
|
} |
|
132
|
|
|
$genre->setName($to)->setTranslatableLocale('en'); |
|
133
|
|
|
$this->em->persist($genre); |
|
134
|
|
|
} |
|
135
|
|
|
// new genre |
|
136
|
|
|
$genre = new Genre(); |
|
137
|
|
|
$genre->setName('Mystery play')->setTranslatableLocale('en'); |
|
138
|
|
|
$this->em->persist($genre); |
|
139
|
|
|
$this->em->flush(); |
|
140
|
|
|
$genre->setName('Мистерия')->setTranslatableLocale('ru'); |
|
141
|
|
|
$this->em->persist($genre); |
|
142
|
|
|
|
|
143
|
|
|
// rename russian |
|
144
|
|
|
$genre = $rep->findOneBy(['name' => 'History']); |
|
145
|
|
|
$genre->setName('История')->setTranslatableLocale('ru'); |
|
146
|
|
|
$this->em->persist($genre); |
|
147
|
|
|
|
|
148
|
|
|
$genre = $rep->findOneBy(['name' => 'War']); |
|
149
|
|
|
$genre->setName('Война')->setTranslatableLocale('ru'); |
|
150
|
|
|
$this->em->persist($genre); |
|
151
|
|
|
|
|
152
|
|
|
$this->em->flush(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: