使用带有Doctrine 2的灯具时出现致命错误

时间:2012-01-30 16:15:08

标签: php symfony doctrine-orm

我是一名Symblog 2初学者,我正在关注Symblog2的这个教程:http://tutorial.symblog.co.uk/docs/doctrine-2-the-blog-model.html

我创建了我的数据模型,并尝试使用Doctrine 2灯具将测试数据填充到我的数据库中。

我下载了必要的软件包并将以下内容添加到我的autoload.php

'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib', 'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',

以及AppKernel.php的后续内容:

new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(),

我的灯具类看起来像这样:

<?php
namespace Soccer\MainBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Soccer\MainBundle\Entity\Team;

class TeamFixtures implements FixtureInterface
{
    public function load($manager)
    {
        $team1 = new Team();
        $team1->setName('Poland');
        $team1->setImg('./img/POL.png');
        $team1->setKitHome('./img/POL_1.png');
        $team1->setKitAway('./img/POL_2.png');
        $manager->persist($team1);

        $manager->flush();
    }
}

当我尝试运行php app/console doctrine:fixtures:load时,我遇到以下异常:

Fatal error: Declaration of Soccer\MainBundle\DataFixtures\ORM\TeamFixtures::load()must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load() in D:\xampp\htdocs\soccertips\em-symfony\src\Soccer\MainBundle\DataFixtures\ORM\TeamFixtures.php on line 8

Call Stack:
    0.0004     328688   1. {main}() D:\xampp\htdocs\soccertips\em-symfony\app\console:0
    0.0283    2043272   2. Symfony\Component\Console\Application->run() D:\xampp\htdocs\soccertips\em-symfony\app\console:22
    0.0344    2230520   3. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() D:\xampp\htdocs\soccertips\em-symfony\vendor\symfony\src\Symfony\Component\Console\Application.php:118
    3.3961   18394992   4. Symfony\Component\Console\Application->doRun() D:\xampp\htdocs\soccertips\em-symfony\vendor\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php:75
    3.3998   18394992   5. Symfony\Component\Console\Command\Command->run() D:\xampp\htdocs\soccertips\em-symfony\vendor\symfony\src\Symfony\Component\Console\Application.php:194
    3.4006   18395336   6. Symfony\Bundle\DoctrineFixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute() D:\xampp\htdocs\soccertips\em-symfony\vendor\symfony\src\Symfony\Component\Console\Command\Command.php:224
    3.4056   18499160   7. Doctrine\Common\DataFixtures\Loader->loadFromDirectory() D:\xampp\htdocs\soccertips\em-symfony\vendor\bundles\Symfony\Bundle\DoctrineFixturesBundle\Command\LoadDataFixturesDoctrineCommand.php:97
    3.4084   18509624   8. require_once('D:\xampp\htdocs\soccertips\em-symfony\src\Soccer\MainBundle\DataFixtures\ORM\TeamFixtures.php') D:\xampp\htdocs\soccertips\em-symfony\vendor\doctrine-fixtures\lib\Doctrine\Common\DataFixtures\Loader.php:92

我理解错误消息,但在我看来,我的load()方法与FixtureInterface::load兼容。

有人可以告诉我,我错过了什么吗?我一步一步地按照教程。

3 个答案:

答案 0 :(得分:10)

FixtureInterface::load() 方法有一个类型提示,因为v1.0.0-ALPHA2

use Doctrine\Common\Persistence\ObjectManager;

function load(ObjectManager $manager);

答案 1 :(得分:8)

您应该添加ObjectManager依赖项:

use Doctrine\Common\Persistence\ObjectManager;

答案 2 :(得分:0)

由于 gview 建议使用Doctrine \ Common \ Persistence \ ObjectManager,因为function load(ObjectManager $manager); ObjectManager需要知道它所对应的类所在的位置。
谢谢你这帮助我SF2.16

They point this issue out here!