@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.