如何在使用doctrine fixture时获取db中的实体?

时间:2016-01-14 20:37:58

标签: php symfony doctrine-orm doctrine

我一直是我的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));

1 个答案:

答案 0 :(得分:2)

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

`     命名空间AcmeBundle \ DataFixtures \ ORM;

JScrollPane

` 然后在其他fixture文件中,你不像在数据库中那样调用用户,你可以这样做:

`
     $ author = $ this-&gt; getReference(&#39; user1&#39;);

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;
    }
}

`

然后将此 $article = new Article(); $article->addAuthor($author); 对象添加到相关对象。

请注意在此$author灯具中实施OrderedFixtureInterface,并将article设置为高于getOrder()的号码。