2017-12-03 124 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