28

我正在使用 Symfony 3.4.0,我尝试使用以下方式加载固定装置:

php bin/console doctrine:fixtures:load

创建数据时出错,怎么回事?

在此处输入图像描述

4

9 回答 9

49

此命令查找所有标记为 的服务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
    }
}

第二个:doctrine.fixture.orm您需要DataFixturessevice.yml配置中手动标记。

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']
于 2018-01-01T13:06:50.027 回答
10

我尝试了@Alexander 的解决方案,但它对我不起作用。

我通过将标签服务添加到文件包上的类Symfony doc解决了同样的问题:services.yml

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 -> 服务容器 -> 自动配置选项

希望这将是一个替代解决方案

于 2017-12-03T21:53:17.670 回答
4

可重用捆绑包的示例。

src/Acme/Bundle/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/config/services.yml

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

附加您的灯具数据:

php bin/console doctrine:fixtures:load --append
于 2018-01-10T08:13:16.353 回答
3

在 4.0.1 中,我必须实现服务配置以显示 Symfony 我的 DataFixtures 文件夹:

在 config/services.yaml

services:

    ...

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

如果我的班级实施 FixtureInterface 并且没有此配置,如果它是 EXTENDS Fixture

于 2017-12-06T08:30:45.610 回答
2

~/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();
    }
}

问题已解决,需要添加服务:(app/config/services.yml)

services:
    # Product service
    ProductBundle\:
        resource: '../../src/ProductBundle/*'
        exclude: '../../src/ProductBundle/{Entity,Repository,Tests}'
于 2017-12-03T10:39:23.047 回答
1

使用 Doctrine\Bundle\FixturesBundle\Fixture

类 ProductFixture 扩展 Fixture 实现 FixtureInterface

查看文档:http ://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

于 2017-12-03T18:01:18.347 回答
1

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

  • 教义/教义固定装置捆绑:^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 文件中或直接在 “app/config/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

现在尝试执行您的命令以加载您的数据夹具。

于 2018-01-08T08:00:54.127 回答
0

阅读上述评论后,我在@GuRu 答案中找到了解决方案:

第二个:您需要在 sevice.yml 配置中手动将教义.fixture.orm 标记为 DataFixtures ”。

然后在您的灯具类中实现 ORMFixtureInterface。

. 事实上,我们必须在 services.yml 中添加额外的配置来解决它。重要的是要知道,我在 symfony 的 ~3.4 版本中注意到了这个问题。

最良好的问候

于 2018-09-29T13:08:46.453 回答
0

我还必须更新 app/AppKernel.php 并在 bundles 数组中添加以下内容:

new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle()
于 2018-06-24T12:35:39.607 回答