Skip to content
This repository was archived by the owner on Dec 29, 2023. It is now read-only.

Commit e04f5e0

Browse files
committed
Command: Install fixtures for all database
1 parent e7115db commit e04f5e0

File tree

8 files changed

+123
-7
lines changed

8 files changed

+123
-7
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ php bin/console doctrine:database:create
3333
php bin/console doctrine:database:create --connection=second
3434
php bin/console doctrine:migrations:migrate -n
3535

36-
3736
# Optional
38-
php bin/console doctrine:fixtures:load -n
37+
php bin/console app:fixtures
3938
```
4039

4140
For the asset symlink install, launch a terminal on administrator in Windows environment.

src/Command/FixtureCommand.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace App\Command;
3+
4+
use Symfony\Component\Console\Attribute\AsCommand;
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
#[AsCommand(
10+
name: 'app:fixtures',
11+
description: 'Load fixtures for all databases',
12+
)]
13+
class FixtureCommand extends Command
14+
{
15+
// Entity Manager name (groups should be used the same value)
16+
private const LIST_DATABASE = ['default', 'second'];
17+
18+
protected function execute(InputInterface $input, OutputInterface $output): int
19+
{
20+
$console = 'php '.realpath(__DIR__.'/../../bin/console');
21+
$command = 'doctrine:fixtures:load';
22+
23+
// No working with official solution, the arguments in ArrayInput are not recognise!!
24+
// $symfonyCommand = $this->getApplication()->find($command);
25+
// $symfonyCommand->run(new ArrayInput(['--em' => 'default', '--group' => 'default', '--no-interaction' => null]), $output);
26+
27+
foreach (self::LIST_DATABASE as $database) {
28+
$output->write('Install fixtures for database: '.$database);
29+
$output->writeln((string) shell_exec("{$console} {$command} --em={$database} --group={$database} --no-interaction"));
30+
}
31+
32+
return Command::SUCCESS;
33+
}
34+
}

src/DataFixtures/CategoryFixtures.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
2+
23
namespace App\DataFixtures;
34

45
use App\Entity\Category;
56
use Doctrine\Bundle\FixturesBundle\Fixture;
7+
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
68
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
79
use Doctrine\Persistence\ObjectManager;
810
use Faker\Factory as FakerFactory;
911
use Symfony\Component\HttpFoundation\File\UploadedFile;
1012

11-
class CategoryFixtures extends Fixture implements DependentFixtureInterface
13+
class CategoryFixtures extends Fixture implements DependentFixtureInterface, FixtureGroupInterface
1214
{
1315
public const NB_FIXTURE = 7;
1416
private \Faker\Generator $faker;
@@ -75,4 +77,12 @@ public function getDependencies(): array
7577
DossierFixtures::class,
7678
];
7779
}
80+
81+
/**
82+
* @return string[]
83+
*/
84+
public static function getGroups(): array
85+
{
86+
return ['default'];
87+
}
7888
}

src/DataFixtures/ClientFixtures.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2+
23
namespace App\DataFixtures;
34

45
use App\Entity\Client;
56
use Doctrine\Bundle\FixturesBundle\Fixture;
7+
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
68
use Doctrine\Persistence\ObjectManager;
79
use Faker\Factory as FakerFactory;
810

9-
class ClientFixtures extends Fixture
11+
class ClientFixtures extends Fixture implements FixtureGroupInterface
1012
{
1113
public const NB_FIXTURE = 10;
1214
private \Faker\Generator $faker;
@@ -33,4 +35,12 @@ public function load(ObjectManager $manager): void
3335

3436
$manager->flush();
3537
}
38+
39+
/**
40+
* @return string[]
41+
*/
42+
public static function getGroups(): array
43+
{
44+
return ['default'];
45+
}
3646
}

src/DataFixtures/DossierFixtures.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
2+
23
namespace App\DataFixtures;
34

4-
use App\Entity\{Client, Dossier, User};
55
use App\Entity\Enum\DossierStatusEnum;
6+
use App\Entity\{Client, Dossier, User};
67
use Doctrine\Bundle\FixturesBundle\Fixture;
8+
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
79
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
810
use Doctrine\Persistence\ObjectManager;
911
use Faker\Factory as FakerFactory;
1012

11-
class DossierFixtures extends Fixture implements DependentFixtureInterface
13+
class DossierFixtures extends Fixture implements DependentFixtureInterface, FixtureGroupInterface
1214
{
1315
public const NB_FIXTURE = 20;
1416
private \Faker\Generator $faker;
@@ -58,4 +60,12 @@ public function getDependencies(): array
5860
UserFixtures::class,
5961
];
6062
}
63+
64+
/**
65+
* @return string[]
66+
*/
67+
public static function getGroups(): array
68+
{
69+
return ['default'];
70+
}
6171
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\DataFixtures;
4+
5+
use App\Entity2\Todolist;
6+
use Doctrine\Bundle\FixturesBundle\{Fixture, FixtureGroupInterface};
7+
use Doctrine\Persistence\ObjectManager;
8+
use Faker\Factory as FakerFactory;
9+
10+
class TodolistFixtures extends Fixture implements FixtureGroupInterface
11+
{
12+
public const NB_FIXTURE = 5;
13+
private \Faker\Generator $faker;
14+
15+
public function __construct()
16+
{
17+
$this->faker = FakerFactory::create();
18+
}
19+
20+
public function load(ObjectManager $manager): void
21+
{
22+
for ($i = 0; $i < self::NB_FIXTURE; ++$i) {
23+
$todolist = (new Todolist)
24+
->setTitle($this->faker->unique()->title())
25+
->setDescription($this->faker->text())
26+
;
27+
28+
$manager->persist($todolist);
29+
$this->setReference("todolist_$i", $todolist);
30+
}
31+
32+
$manager->flush();
33+
}
34+
35+
/**
36+
* @return string[]
37+
*/
38+
public static function getGroups(): array
39+
{
40+
return ['second'];
41+
}
42+
}

src/DataFixtures/UserFixtures.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2+
23
namespace App\DataFixtures;
34

45
use App\Entity\User;
56
use Doctrine\Bundle\FixturesBundle\Fixture;
7+
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
68
use Doctrine\Persistence\ObjectManager;
79
use Faker\Factory as FakerFactory;
810

9-
class UserFixtures extends Fixture
11+
class UserFixtures extends Fixture implements FixtureGroupInterface
1012
{
1113
public const USERS = [
1214
'admin' => ['enabled' => true, 'roles' => [User::ROLE_ADMIN]],
@@ -40,4 +42,12 @@ public function load(ObjectManager $manager): void
4042

4143
$manager->flush();
4244
}
45+
46+
/**
47+
* @return string[]
48+
*/
49+
public static function getGroups(): array
50+
{
51+
return ['default'];
52+
}
4353
}

src/Entity2/Todolist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Validator\Constraints as Assert;
99

1010
#[ORM\Entity(repositoryClass: \App\Repository2\TodolistRepository::class)]
11+
#[ORM\HasLifecycleCallbacks]
1112
class Todolist
1213
{
1314
#[ORM\Id, ORM\GeneratedValue, ORM\Column]

0 commit comments

Comments
 (0)