【发布时间】: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