|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Command\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Application\Entity\UserEntity; |
|
6
|
|
|
use Application\Entity\ProfileEntity; |
|
7
|
|
|
use Silex\Application; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Borut Balažek <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class HydrateDataCommand extends ContainerAwareCommand |
|
17
|
|
|
{ |
|
18
|
|
|
protected $app; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($name, Application $app) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct($name); |
|
23
|
|
|
|
|
24
|
|
|
$this->app = $app; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function configure() |
|
28
|
|
|
{ |
|
29
|
|
|
$this |
|
30
|
|
|
->setName( |
|
31
|
|
|
'application:database:hydrate-data' |
|
32
|
|
|
) |
|
33
|
|
|
->setDescription('Add an Test User to the database') |
|
34
|
|
|
->addOption( |
|
35
|
|
|
'remove-existing-data', |
|
36
|
|
|
'r', |
|
37
|
|
|
InputOption::VALUE_NONE, |
|
38
|
|
|
'When the existing data should be removed' |
|
39
|
|
|
) |
|
40
|
|
|
; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
44
|
|
|
{ |
|
45
|
|
|
$app = $this->app; |
|
46
|
|
|
|
|
47
|
|
|
$removeExistingData = $input->getOption('remove-existing-data'); |
|
48
|
|
|
|
|
49
|
|
|
if ($removeExistingData) { |
|
50
|
|
|
try { |
|
51
|
|
|
$app['db']->query('SET foreign_key_checks = 0;'); |
|
52
|
|
|
|
|
53
|
|
|
$tables = $app['db']->getSchemaManager()->listTables(); |
|
54
|
|
|
|
|
55
|
|
|
foreach ($tables as $table) { |
|
56
|
|
|
$table = $table->getName(); |
|
57
|
|
|
|
|
58
|
|
|
$app['db']->query('TRUNCATE TABLE '.$table.';'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$app['db']->query('SET foreign_key_checks = 1;'); |
|
62
|
|
|
|
|
63
|
|
|
$output->writeln('<info>All tables were successfully truncated!</info>'); |
|
64
|
|
|
} catch (\Exception $e) { |
|
65
|
|
|
$output->writeln('<error>'.$e->getMessage().'</error>'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/***** Users *****/ |
|
70
|
|
|
$users = include APP_DIR.'/fixtures/users.php'; |
|
71
|
|
|
|
|
72
|
|
|
foreach ($users as $user) { |
|
73
|
|
|
$userEntity = new UserEntity(); |
|
74
|
|
|
$profileEntity = new ProfileEntity(); |
|
75
|
|
|
|
|
76
|
|
|
// Profile |
|
77
|
|
|
$profileEntity |
|
78
|
|
|
->setFirstName($user['profile']['firstName']) |
|
79
|
|
|
->setLastName($user['profile']['lastName']) |
|
80
|
|
|
; |
|
81
|
|
|
|
|
82
|
|
|
if (isset($user['profile']['gender'])) { |
|
83
|
|
|
$profileEntity |
|
84
|
|
|
->setGender($user['profile']['gender']) |
|
85
|
|
|
; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (isset($user['profile']['birthdate'])) { |
|
89
|
|
|
$profileEntity |
|
90
|
|
|
->setBirthdate($user['profile']['birthdate']) |
|
91
|
|
|
; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// User |
|
95
|
|
|
$userEntity |
|
96
|
|
|
->setId($user['id']) |
|
97
|
|
|
->setUsername($user['username']) |
|
98
|
|
|
->setEmail($user['email']) |
|
99
|
|
|
->setPlainPassword( |
|
100
|
|
|
$user['plainPassword'], |
|
101
|
|
|
$app['security.encoder_factory'] |
|
102
|
|
|
) |
|
103
|
|
|
->setRoles($user['roles']) |
|
104
|
|
|
->setProfile($profileEntity) |
|
105
|
|
|
->enable() |
|
106
|
|
|
; |
|
107
|
|
|
|
|
108
|
|
|
$app['orm.em']->persist($userEntity); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
$app['orm.em']->flush(); |
|
113
|
|
|
|
|
114
|
|
|
$output->writeln('<info>Data was successfully hydrated!</info>'); |
|
115
|
|
|
} catch (\Exception $e) { |
|
116
|
|
|
$output->writeln('<error>'.$e->getMessage().'</error>'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|