【问题标题】:How to fetch entities in db while using doctrine fixtures?如何在使用学说夹具时获取数据库中的实体?
【发布时间】:2016-01-14 20:37:58
【问题描述】:

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

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

<?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));

【问题讨论】:

  • 你想做什么?你知道什么是学说数据固定装置吗?
  • 我正在尝试获取所有用户并在将它们插入数据库之前使用它们来补充我的广告。
  • 你的课程应该扩展use Symfony\Bundle\FrameworkBundle\Controller\Controller; ?
  • 如果您添加一个用户夹具或在此夹具中创建用户,您将获得更好的服务 - 这将进一步将您的测试与您的开发和产品数据库分开。
  • 我在考虑这个,geoB。问题是我正在使用 FOSUserBundle 并且不知道如何为它创建数据固定装置。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

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

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 更大的数字。

【讨论】:

  • 完美!非常感谢您,Zed 勋爵!
  • 没问题,很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多