I have been the ContainerAwareInterface on my LoadAdvert class so I can call and use the EntityManager to retrieve users in the database.
However, when executing php bin/console doctrine:fixtures:load, the entity manager retrieves only an empty array, as if there was no user in the db.
<?php // src/OC/PlatformBundle/DataFixtures/ORM/LoadCategory.php namespace OCPlatformBundleDataFixturesORM; use DoctrineCommonDataFixturesFixtureInterface; use DoctrineCommonPersistenceObjectManager; use SymfonyComponentDependencyInjectionContainerAwareInterface; use SymfonyComponentDependencyInjectionContainerInterface; use OCPlatformBundleEntityAdvert; use OCUserBundleEntityUser; class LoadAdvert implements FixtureInterface, ContainerAwareInterface { /** * @var ContainerInterface */ private $container; public function setContainer(ContainerInterface $container = null) { $this->container = $container; } // Dans l'argument de la méthode load, l'objet $manager est l'EntityManager public function load(ObjectManager $manager) { $em = $this->container->get('doctrine.orm.entity_manager'); $author = $em->getRepository('OCUserBundle:User')->findAll(); die(var_dump($author));
Advertisement
Answer
Here it is how I do it. The main thing is to set reference for user objects that you create and then call it in other fixture file and assign them to related objects. Also its important the order of execution of fixture data. You cant load related object before you load users.
namespace AcmeBundleDataFixturesORM; use DoctrineCommonDataFixturesAbstractFixture; use DoctrineCommonDataFixturesOrderedFixtureInterface; use DoctrineCommonPersistenceObjectManager; use SymfonyComponentDependencyInjectionContainerAwareInterface; use SymfonyComponentDependencyInjectionContainerInterface; class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface { /** @var ContainerInterface */ private $container; /** * Load data fixtures with the passed EntityManager * * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $userManager = $this->container->get('fos_user.user_manager'); $user1 = $userManager->createUser(); $user1->setUsername('username1'); $user1->setPlainPassword('123'); $user1->setEmail('example1@gmail.com'); $user1->setEnabled(true); $user2 = $userManager->createUser(); $user2->setUsername('username2'); $user2->setPlainPassword('123'); $user1->setEmail('example2@gmail.com'); $user2->setEnabled(true); $manager->persist($user1); $manager->persist($user2); $this->setReference('user1', $user1); $this->setReference('user2', $user2); $manager->flush(); } /** * Get the order of this fixture * * @return integer */ public function getOrder() { return 1; } /** * Sets the Container. Need it to create new User from fos_user.user_manager service * * @param ContainerInterface|null $container A ContainerInterface instance or null */ public function setContainer(ContainerInterface $container = null) { $this->container = $container; } }
Then in other fixture file you dont call user as you did from database, you do it like this:
$author = $this->getReference('user1'); $article = new Article(); $article->addAuthor($author);
and then you add this $author
object to related object.
Keep in mind to implement OrderedFixtureInterface
in this article
fixture and set getOrder()
to be higher number than the one for users
.