如何在使用原则夹具时获取数据库中的实体


How to fetch entities in db while using doctrine fixtures?

我一直是LoadAdvert类上的ContainerAwareInterface,所以我可以调用并使用EntityManager来检索数据库中的用户。

但是,当执行 php bin/console doctrine:fixtures:load 时,实体管理器只检索一个空数组,就好像数据库中没有用户一样。

<?php
// src/OC/PlatformBundle/DataFixtures/ORM/LoadCategory.php
namespace OC'PlatformBundle'DataFixtures'ORM;
use Doctrine'Common'DataFixtures'FixtureInterface;
use Doctrine'Common'Persistence'ObjectManager;
use Symfony'Component'DependencyInjection'ContainerAwareInterface;
use Symfony'Component'DependencyInjection'ContainerInterface;
use OC'PlatformBundle'Entity'Advert;
use OC'UserBundle'Entity'User;
   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));

这就是我的做法。最主要的是为您创建的用户对象设置引用,然后在其他夹具文件中调用它并将它们分配给相关对象。夹具数据的执行顺序也很重要。在加载用户之前,无法加载相关对象。

namespace AcmeBundle'DataFixtures'ORM;
use Doctrine'Common'DataFixtures'AbstractFixture;
use Doctrine'Common'DataFixtures'OrderedFixtureInterface;
use Doctrine'Common'Persistence'ObjectManager;
use Symfony'Component'DependencyInjection'ContainerAwareInterface;
use Symfony'Component'DependencyInjection'ContainerInterface;
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;
    }
}

然后在其他夹具文件中,您不会像从数据库那样调用用户,而是这样做:

 $author = $this->getReference('user1');
 $article = new Article();
 $article->addAuthor($author);

然后将此$author对象添加到相关对象。

请记住在此article装置中实现OrderedFixtureInterface,并将getOrder()设置为高于users的数字。