|
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 Version20140408113030_AddItemGenres extends AbstractMigration implements ContainerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Entity manager. |
|
26
|
|
|
* |
|
27
|
|
|
* @var EntityManagerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $em; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Add genres. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $genres = [ |
|
37
|
|
|
'Cars' => 'Машины', |
|
38
|
|
|
'Demons' => 'Демоны', |
|
39
|
|
|
'Game' => 'Игры', |
|
40
|
|
|
'Magic' => 'Магия', |
|
41
|
|
|
'Space' => 'Космос', |
|
42
|
|
|
'Super Power' => 'Супер сила', |
|
43
|
|
|
'Harem' => 'Гарем', |
|
44
|
|
|
'Supernatural' => 'Сверхъестественное', |
|
45
|
|
|
'Gender Bender' => 'Смена пола', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set container. |
|
50
|
|
|
* |
|
51
|
|
|
* @param ContainerInterface $container |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setContainer(ContainerInterface $container = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->em = $container->get('doctrine.orm.entity_manager'); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Schema $schema |
|
60
|
|
|
*/ |
|
61
|
|
|
public function up(Schema $schema) |
|
62
|
|
|
{ |
|
63
|
|
|
$rep = $this->em->getRepository('Gedmo\\Translatable\\Entity\\Translation'); |
|
64
|
|
|
foreach ($this->genres as $en => $ru) { |
|
65
|
|
|
$genre = new Genre(); |
|
66
|
|
|
|
|
67
|
|
|
$genre->setName($en)->setTranslatableLocale('en'); |
|
68
|
|
|
$this->em->persist($genre); |
|
69
|
|
|
$this->em->flush($genre); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$rep->translate($genre, 'name', 'ru', $ru); |
|
72
|
|
|
} |
|
73
|
|
|
$this->em->flush(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Schema $schema |
|
78
|
|
|
*/ |
|
79
|
|
|
public function down(Schema $schema) |
|
80
|
|
|
{ |
|
81
|
|
|
$rep = $this->em->getRepository('AnimeDbCatalogBundle:Genre'); |
|
82
|
|
|
foreach (array_keys($this->genres) as $en) { |
|
83
|
|
|
$this->em->remove($rep->findOneBy(['name' => $en])); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: