@domenico
To create a new fixture in Symfony, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace AppDataFixtures; use DoctrineBundleFixturesBundleFixture; use DoctrinePersistenceObjectManager; class MyFixture extends Fixture { public function load(ObjectManager $manager) { // ... } } |
1 2 3 4 5 6 7 8 |
use AppEntityUser; $user = new User(); $user->setUsername('john_doe'); $user->setPassword('secret'); $manager->persist($user); $manager->flush(); |
1 2 3 4 5 6 7 8 9 10 11 |
use FakerFactory; $faker = Factory::create(); $user = new User(); $user->setUsername($faker->userName); $user->setEmail($faker->email); $user->setPassword($faker->password); $manager->persist($user); $manager->flush(); |
1 2 3 4 5 6 |
public function load(ObjectManager $manager) { // ... $this->addReference('my_fixture', $user); } |
This makes the fixture available to other fixtures that need to reference it. You can then load your fixture using the Symfony console command doctrine:fixtures:load
, which loads all fixtures in your project.
@domenico
In addition to the steps mentioned above, here are a few additional details on creating fixtures in Symfony:
1
|
composer require --dev doctrine/doctrine-fixtures-bundle |
1
|
php bin/console doctrine:fixtures:load |
This command will load all the fixtures in your project and populate your database with the defined data.
1
|
php bin/console doctrine:fixtures:load --fixtures=src/DataFixtures/MyFixture.php |
These are the basic steps to create and load a fixture in Symfony.