2017-12-03 126 views
5

,我尝试加载夹具:Symfony的3.4.0找不到任何固定的服务,我使用的Symfony 3.4.0加载

php bin/console doctrine:fixtures:load 

错误而产生的数据时,有什么不对?

enter image description here

+0

你的灯具在哪里?哪个目录?可能会显示代码 –

+0

您的灯具应位于例如'src \ AppBundle \ DataFixtures \ ORM \ MyFixture.php' – baris1892

+0

修补程序:〜/ dev/domain.lan/src/ProductBundle/DataFixtures/ORM/ProductFixture.php 网站根目录:〜/ dev/domain.lan/ – Alexander

回答

1

〜的/ dev/domain.lan/src目录/ ProductBundle/DataFixtures/ORM/ProductF ixture.php

<?php 

namespace ProductBundle\DataFixtures\ORM; 

use Doctrine\Bundle\FixturesBundle\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
use ProductBundle\Entity\Product; 

class ProductFixture implements FixtureInterface 
{ 

    public function load(ObjectManager $manager) 
    { 
     // create 20 products! Bam! 
     for ($i = 0; $i < 20; $i++) { 
      $product = new Product(); 
      $product->setName('Product name' . $i); 
      $manager->persist($product); 
     } 

     $manager->flush(); 
    } 
} 

问题是解决了,有必要增加一个服务:(应用程序/配置/ services.yml)

services: 
    # Product service 
    ProductBundle\: 
     resource: '../../src/ProductBundle/*' 
     exclude: '../../src/ProductBundle/{Entity,Repository,Tests}' 
+0

我是你应该把它加到'services.yml'包文件例如:'〜/ dev/domain.lan/src/ProductBundle/Resoueces/config/services.yml' – zatamine

6

我试图@亚历山大的解决方案,但对我来说这是行不通的。

我已经通过在services.yml文件包中添加的标签服务类,Symfony doc解决同样的问题:

BlogBundle/Resources/config/services.yml

Services: 
... 
# Fixtures services 
    BlogBundle\DataFixtures\ORM\PostFixture: 
     tags: [doctrine.fixture.orm] 
... 

BlogBundle/DataFixtures/ORM/PostFixture.php类:

... 
use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
... 

class PostFixture implements FixtureInterface 
{ 
public function load(ObjectManager $manager) 
    { 
... 
} 
} 

来源灵感:Synfony doc -> Service container -> The autoconfigure Option

希望能对大家的替代解决方案

+1

此解决方案工作正常。当我从v2.4.1升级到3.0.2时,我开始出现这个错误。 – Praveesh

1

在4.0.1我要实现服务配置,显示的Symfony我DataFixtures文件夹:如果

在配置

/services.yaml

services: 

    ... 

    App\DataFixtures\: 
     resource: '../src/DataFixtures' 
     tags: [doctrine.fixture.orm] 

我class IMPLEMENTS FixtureInterface并且没有这个配置,如果它是EXTENDS Fixture

+0

谢谢。这对我有效。 –

+2

@Dimitry,最好使用这个'resource:'%kernel.project_dir%/ src/DataFixtures'' – Mick

9

该命令查找所有标记为doctrine.fixture.orm的服务。
有两种方法可以解决这个问题。

第一招:实现ORMFixtureInterface任何类将自动与此标记注册。

<?php 

namespace AppBundle\DataFixtures\ORM; 


use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 

class LoadFixtures implements ORMFixtureInterface 
{ 
    public function load(ObjectManager $manager) 
    { 
     #your code 
    } 
} 

第二个:您需要手动在sevice.yml配置标记doctrine.fixture.ormDataFixtures

services: 
    ... 

    # makes classes in src/AppBundle/DataFixtures available to be used as services 
    # and have a tag that allows actions to type-hint services 
    AppBundle\DataFixtures\: 
     resource: '../../src/AppBundle/DataFixtures' 
     tags: ['doctrine.fixture.orm'] 
+1

第二个为我工作。 – Sruj

+1

第二个只有工作。谢谢 –

0

经过长期的研究,找到了解决办法。 这项工作有:

  • 学说/教义,灯具束:^ 3.0,
  • Symfony的^ 3.3

首先

  • 定义你的灯具。
<?php 
namespace Where\MyFixtureBundle\FileFolder\IsLocated; 

use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
use Nao\UserBundle\Entity\User; 

class LoadData implements FixtureInterface 
{ 
    /** 
    * Load data fixtures with the passed EntityManager 
    * 
    * @param ObjectManager $manager 
    */ 
    public function load(ObjectManager $manager){ 
     $object = new Entity(); 
     $object->setFoo(bar) 
     ... 

     $manager->persist($object); 

     $manager->flush(); 
    } 
} 

接着,在bundle的service.yml文件或直接在 限定服务 “应用程序/配置/ service.yml”(未建议报告)

# Fixtures service 
your_service.name: 
    class: Full\Namespce\With\TheClassFixtureName 
    tags: [doctrine.fixture.orm] <-- important 

不要忘了,只是要确定以下

composer du -o or composer dump-autoload -o 

现在尝试执行你的命令加载数据的灯具。

1

可重复使用包的示例。

的src/Acme公司/包/ UserBundle/DataFixtures/ORM/DataFixtures.php

<?php 
namespace Acme\Bundle\UserBundle\DataFixtures\ORM; 

use Doctrine\Bundle\FixturesBundle\Fixture; 
use Doctrine\Common\Persistence\ObjectManager; 

class DataFixtures extends Fixture 
{ 
    /** 
    * Load data fixtures with the passed EntityManager 
    * 
    * @param ObjectManager $manager 
    */ 
    public function load(ObjectManager $manager) 
    { 
     #your code 
    } 
} 
在app

/配置/ services.yml

Acme\Bundle\UserBundle\DataFixtures\: 
    resource: '../../src/Acme/Bundle/UserBundle/DataFixtures/' 

添加您的灯具数据:

php bin/console doctrine:fixtures:load --append