【问题标题】:Symfony 2 - couldn't load a fixtureSymfony 2 - 无法加载夹具
【发布时间】:2018-05-16 09:16:24
【问题描述】:

我是 Symfony 2(2.8.* 版本)的初学者。我正在尝试使用fixture 和faker 将示例数据加载到我的数据库中。我已经创建了 src/AppBundle/DataFixtures/ORM 目录,并在其中放置了一个带有以下代码的 LoadPostData.php 文件:

<?php

namespace AppBundle\DataFixtures\ORM;


use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistance\ObjectManager;


class LoadPostData implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $faker = \Faker\Factory::create();

        for ($i = 1; $i < 200; $i++) {

            $post = new \AppBundle\Entity\Post();
            $post->setTitle($faker->sentence(3));
            $post->setLead($faker->text(300));
            $post->setContent($faker->text(800));
            $post->setCreatedAt($faker->dateTimeThisMonth);


            $manager->persist($post);
        }


        $manager->flush();
    }
}

但是当我在终端中点击命令“php app/console dictionary:fixtures:load”时,我得到了这个错误:

PHP Fatal error:  Declaration of AppBundle\DataFixtures\ORM\LoadPostData::load(Doctrine\Common\Persistance\O
bjectManager $manager) must be compatible with Doctrine\Common\DataFixtures\FixtureInterface::load(Doctrine\Common\Persist
ence\ObjectManager $manager) in /Users/myMac/Desktop/symfony2/Blog/src/AppBundle/DataFixtures/ORM/
LoadPostData.php on line 10

Fatal error: Declaration of AppBundle\DataFixtures\ORM\LoadPostData::load(Doctrine\Common\Persistance\ObjectManager $manager) must be compatible with Doctrine\Common\DataFixtures\FixtureInterface::load(Doctrine\Common\Persistence\ObjectManager $manager) in /Users/myMac/Desktop/symfony2/Blog/src/AppBundle/DataFixtures/ORM/LoadPostData.php on line 10

(第10行是LoadPostData类的声明)

我在这里做错了什么?我正在逐步遵循教程,但不知道缺少什么。提前致谢!

【问题讨论】:

  • 你的load函数应该是公开的。
  • 对。我已经解决了这个问题,但我仍然得到和以前一样的错误
  • 你在“Persistance”中也有一个错字,叫做“Persistence”:use Doctrine\Common\Persistence\ObjectManager;

标签: php symfony symfony-2.8


【解决方案1】:

从错误消息中获取函数调用揭示了你的错误:

致命错误:声明 AppBundle\DataFixtures\ORM\LoadPostData::load(Doctrine\Common\Persistance\ObjectManager $manager) 必须兼容 Doctrine\Common\DataFixtures\FixtureInterface::load(Doctrine\Common\Persistence\ObjectManager $经理)在 /Users/myMac/Desktop/symfony2/Blog/src/AppBundle/DataFixtures/ORM/LoadPostData.php 在第 10 行

这两个声明是:

::load(Doctrine\Common\Persistance\ObjectManager $manager)
::load(Doctrine\Common\Persistence\ObjectManager $manager)

您在use 语句中拼错了Persistence

【讨论】:

  • 那是我的错误。非常感谢您帮助我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 2016-10-12
  • 2013-05-06
  • 1970-01-01
相关资源
最近更新 更多