Skip to content

Commit a757932

Browse files
authored
Merge pull request #287 from dotkernel/issue-286
Implemented doctrine data fixtures
2 parents 74ca78d + 7400ad5 commit a757932

7 files changed

Lines changed: 73 additions & 31 deletions

File tree

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,7 @@ Note: you need to whitelist `localhost` in the reCAPTCHA settings page during de
141141
142142
## Migrations
143143
144-
Run the migrations and seeds with these commands:
145-
146-
```bash
147-
php vendor/bin/phinx migrate --configuration="config/migrations.php"
148-
php vendor/bin/phinx seed:run --configuration="config/migrations.php"
149-
```
150-
151-
### Migration alternative with Doctrine
152-
Out of the box, we use Phinx like detailed above to populate the database. Doctrine is an alternative that is also ready to use for new migrations. An example file is included in `/data/doctrine/migrations`, but it's empty, so it won't run any queries. It can be edited freely. To generate a new migration file, use this command
144+
Out of the box, we use Doctrine Migrations like detailed below to populate the database. An example file is included in `/data/doctrine/migrations`. To generate a new migration file, use this command:
153145
154146
```bash
155147
php vendor/bin/doctrine-migrations migrations:generate
@@ -192,6 +184,20 @@ php vendor/bin/doctrine-migrations migrations:execute --down 20220606131835
192184
This will also remove the log for that migration in the database, allowing the migration to run again with `php vendor/bin/doctrine-migrations migrate`.
193185
Note the `20220606131835` is taken from the migration filename, e.g. `Version20220606131835.php`
194186
187+
## Seeding the database (Fixtures)
188+
Seeding the database is done with the help of our custom package ``dotkernel/dot-data-fixtures`` built on top of doctrine/data-fixtures. See below on how to use our CLI command for listing and executing Doctrine data fixtures.
189+
190+
An example of a fixtures class is ``data/doctrine/fixtures/RoleLoader.php``
191+
192+
Running ``php bin/doctrine fixtures:list`` will list all the available fixtures, by order of execution.
193+
194+
To execute all fixtures run : ``php bin/doctrine fixtures:execute``.
195+
196+
To execute a specific fixtures run : ``php bin/doctrine fixtures:execute --class=RoleLoader``
197+
198+
Fixtures can and should be ordered to ensure database consistency, more on ordering fixtures can be found here :
199+
https://www.doctrine-project.org/projects/doctrine-data-fixtures/en/latest/how-to/fixture-ordering.html#fixture-ordering
200+
195201
## Development mode
196202
197203
- If you use `composer create-project`, the project will go into development mode automatically after installing. The development mode status can be checked and toggled by using these composer commands

bin/doctrine

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Doctrine\ORM\Tools\Console\ConsoleRunner;
4+
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
5+
6+
require_once 'vendor/autoload.php';
7+
8+
$container = require getcwd() . '/config/container.php' ;
9+
10+
$entityManager = $container->get(\Doctrine\ORM\EntityManager::class);
11+
12+
$commands = [
13+
$container->get(Dot\DataFixtures\Command\ExecuteFixturesCommand::class),
14+
$container->get(Dot\DataFixtures\Command\ListFixturesCommand::class),
15+
];
16+
17+
ConsoleRunner::run(
18+
new SingleManagerProvider($entityManager),
19+
$commands
20+
);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"dotkernel/dot-response-header": "^3.1.0",
5757
"dotkernel/dot-session": "^4.3.0",
5858
"dotkernel/dot-twigrenderer": "^3.2.1",
59+
"dotkernel/dot-data-fixtures": "^1.0",
5960
"laminas/laminas-component-installer": "^2.8.0",
6061
"laminas/laminas-config-aggregator": "^1.8.0",
6162
"laminas/laminas-i18n": "^2.17.0",

config/autoload/doctrine.global.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
'class' => \Doctrine\Common\Cache\PhpFileCache::class,
5757
'directory' => getcwd() . '/data/cache/doctrine'
5858
]
59-
]
59+
],
60+
'fixtures' => getcwd() . '/data/doctrine/fixtures',
6061
],
6162
'resultCacheLifetime' => 600
6263
];

config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class_exists(\Mezzio\Swoole\ConfigProvider::class)
4141
\Dot\Rbac\ConfigProvider::class,
4242
\Dot\Rbac\Guard\ConfigProvider::class,
4343
\Dot\ResponseHeader\ConfigProvider::class,
44+
\Dot\DataFixtures\ConfigProvider::class,
4445

4546
// Default App module config
4647
\Frontend\App\ConfigProvider::class,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Frontend\Fixtures;
4+
5+
use Doctrine\Common\DataFixtures\FixtureInterface;
6+
use Doctrine\Persistence\ObjectManager;
7+
use Frontend\User\Entity\UserRole;
8+
9+
/**
10+
* Class RoleLoader
11+
* @package Frontend\Fixtures
12+
*/
13+
class RoleLoader implements FixtureInterface
14+
{
15+
public function load(ObjectManager $manager): void
16+
{
17+
$adminRole = new UserRole();
18+
$adminRole->setName('admin');
19+
20+
$userRole = new UserRole();
21+
$userRole->setName('user');
22+
23+
$guestRole = new UserRole();
24+
$guestRole->setName('guest');
25+
26+
$manager->persist($adminRole);
27+
$manager->persist($userRole);
28+
$manager->persist($guestRole);
29+
30+
$manager->flush();
31+
}
32+
}
33+

data/doctrine/migrations/Version20220817121035.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,5 @@ public function down(Schema $schema): void
5757
$this->addSql('DROP TABLE user_reset_password');
5858
$this->addSql('DROP TABLE user_role');
5959
}
60-
61-
public function postUp(Schema $schema): void
62-
{
63-
$this->connection->insert('user_role', [
64-
'uuid' => UuidOrderedTimeGenerator::generateUuid()->getBytes(),
65-
'name' => UserRole::ROLE_ADMIN,
66-
'created' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
67-
]);
68-
69-
$this->connection->insert('user_role', [
70-
'uuid' => UuidOrderedTimeGenerator::generateUuid()->getBytes(),
71-
'name' => UserRole::ROLE_USER,
72-
'created' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
73-
]);
74-
75-
$this->connection->insert('user_role', [
76-
'uuid' => UuidOrderedTimeGenerator::generateUuid()->getBytes(),
77-
'name' => UserRole::ROLE_GUEST,
78-
'created' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
79-
]);
80-
}
8160
}
61+

0 commit comments

Comments
 (0)