1

我想为我的项目创建 som 固定装置以进行测试。我现在在命令行脚本中执行此操作,但我从 Doctrine 中发现了 DataFixtures 捆绑包并想对其进行测试。我有以下代码:

<?php
// src/Pan100/MoodLogBundle/DataFixtures/ORM/LoadUserData.php

namespace Acme\HelloBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAware;

use Pan100\MoodLogBundle\Entity\Day;
use Pan100\MoodLogBundle\Entity\Medication;
use Pan100\MoodLogBundle\Entity\Trigger;

use FOS\UserBundle\Doctrine\UserManager;

class MockDataMaker extends ContainerAware
{
    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $userManager = $container->get('fos_user.user_manager');
        //make some users
        $user = $userManager->createUser();
        $user->setUsername('John');
        $user->setEmail('john.doe@example.com');
        $user->setPlainPassword('passord');
        $userManager->updateUser($user);
    }

}

我明白了:

  [InvalidArgumentException]                                                   
  Could not find any fixtures to load in:                                      

  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/D  
  ataFixtures/ORM                                                              
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Da  
  taFixtures/ORM                                                               
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/DataFi  
  xtures/ORM                                                                   
  - /var/www/path/vendor/symfony/monolog-bundle/Symfony/Bundle/MonologBundle/  
  DataFixtures/ORM                                                             
  - /var/www/path/vendor/symfony/swiftmailer-bundle/Symfony/Bundle/Swiftmaile  
  rBundle/DataFixtures/ORM                                                     
  - /var/www/path/vendor/symfony/assetic-bundle/Symfony/Bundle/AsseticBundle/  
  DataFixtures/ORM                                                             
  - /var/www/path/vendor/doctrine/doctrine-bundle/Doctrine/Bundle/DoctrineBun  
  dle/DataFixtures/ORM                                                         
  - /var/www/path/vendor/sensio/framework-extra-bundle/Sensio/Bundle/Framewor  
  kExtraBundle/DataFixtures/ORM                                                
  - /var/www/path/vendor/jms/aop-bundle/JMS/AopBundle/DataFixtures/ORM         
  - /var/www/path/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/DataFixtures/O  
  RM                                                                           
  - /var/www/path/vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Da  
  taFixtures/ORM                                                               
  - /var/www/path/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/DataFixt  
  ures/ORM                                                                     
  - /var/www/path/src/Pan100/MoodLogBundle/DataFixtures/ORM                    
  - /var/www/path/vendor/sonata-project/intl-bundle/Sonata/IntlBundle/DataFix  
  tures/ORM                                                                    
  - /var/www/path/vendor/ob/highcharts-bundle/Ob/HighchartsBundle/DataFixture  
  s/ORM                                                                        
  - /var/www/path/vendor/doctrine/doctrine-fixtures-bundle/Doctrine/Bundle/Fi  
  xturesBundle/DataFixtures/ORM                                                
  - /var/www/path/src/Acme/DemoBundle/DataFixtures/ORM                         
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle  
  /DataFixtures/ORM                                                            
  - /var/www/path/vendor/sensio/distribution-bundle/Sensio/Bundle/Distributio  
  nBundle/DataFixtures/ORM                                                     
  - /var/www/path/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundl  
  e/DataFixtures/ORM

我的问题是:如何使用 FOSUserBundle 在灯具中创建用户?

4

2 回答 2

6

您的课程必须扩展“AbstractFixture”并实现“ContainerAwareInterface”以访问用户管理器服务!如果您的类不扩展“AbstractFixture”,则不是固定装置;-) 在 fosub 和 symfony 中寻找不同的固定装置接口和不同的容器感知接口以获取更多方法。验证命名空间是否是复制并粘贴 Sadden,因为我的英语真的不差!例如:对于订购的灯具

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\Persistence\ObjectManager;
class LoadArticleFixture extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
 * @var ContainerInterface
 */
private $container;

/**
 * {@inheritDoc}
 */
public function setContainer(ContainerInterface $container = null)
{
    $this->container = $container;
}
public function load(ObjectManager $manager)
{
    $auteurs = $this->container->get('fos_user.user_manager')->findUsers();
    etc...
}
于 2013-05-28T15:10:55.220 回答
1

您的源文件位置与您的命名空间不匹配:

src/Pan100/MoodLogBundle/DataFixtures/ORM/LoadUserData.php对比namespace Acme\HelloBundle\DataFixtures\ORM;

尝试将您的命名空间设置为Pan100\MoodLogBundle\DataFixtures\ORM.

正如 MolecularMan 指出的那样,您还应该实施FixtureInterface

于 2013-03-25T12:35:54.523 回答