我是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 doctrine: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类的声明)
我在这里做错了什么?我一步一步地按照教程,不知道缺少什么。提前致谢!
答案 0 :(得分:1)
从错误消息中抓取函数调用会显示您的错误:
致命错误:声明 的appbundle \ DataFixtures \ ORM \的LoadPostData ::负载(教义\ COMMON \持久性\的ObjectManager $ manager)必须兼容 学说\ COMMON \ DataFixtures \ FixtureInterface ::负载(教义\ COMMON \持久性\的ObjectManager $经理) /Users/myMac/Desktop/symfony2/Blog/src/AppBundle/DataFixtures/ORM/LoadPostData.php 在第10行
2个声明是:
::load(Doctrine\Common\Persistance\ObjectManager $manager)
::load(Doctrine\Common\Persistence\ObjectManager $manager)
您在Persistence
声明中拼错了use
。