【问题标题】:Fatal Error when using Fixtures with Doctrine 2将 Fixtures 与 Doctrine 2 一起使用时出现致命错误
【发布时间】:2012-02-22 10:21:33
【问题描述】:

我是 Symblog 2 初学者,我正在关注 this tutorial for Symblog2。

我创建了我的数据模型,并尝试使用 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 时,我收到以下异常:

致命错误:Soccer\MainBundle\DataFixtures\ORM\TeamFixtures::load() 的声明必须与 Doctrine\Common\DataFixtures\FixtureInterface::load() 的声明兼容 在 D:\xampp\htdocs\soccertips\em-symfony\src\Soccer\MainBundle\DataFixtures\ORM\TeamFixtures.php 行 8 中 p>

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 兼容。

谁能告诉我,我错过了什么?我是按照教程一步一步来的。

【问题讨论】:

  • 您下载了相互不兼容的组件。仔细检查你下载了正确的代码和正确的版本/修订。
  • 我用php bin/vendors install命令从git下载了最新版本,所以我认为,版本应该兼容
  • 好吧,想想你想要什么,需要执行这些东西的 PHP 本身会告诉你这些东西不能一起工作。因此,由于 PHP 需要完成这项工作,可能更好地信任 PHP?另外我会仔细检查自动加载器的配置,不是所有相同的命名空间部分都共享同一个目录,而它们来自同一个供应商,不是吗?我可能是错的,但这看起来有点可疑。
  • 好吧,我发现,该学说希望将加载函数定义为 public function load(ObjectManager $manager) 而不是 function load($manager)。感谢您的帮助。
  • 如果是教程代码,那么使用教程中给出的版本(或更改您的教程代码副本)。只要你不修改库第三方代码,我就不会费心了。该教程可能已经过时了。请添加您的解决方案作为答案,其他人也可能使用该教程并遇到同样的问题。

标签: php symfony doctrine-orm


【解决方案1】:

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

use Doctrine\Common\Persistence\ObjectManager;

function load(ObjectManager $manager);

【讨论】:

  • 谢谢,我已经想通了,但不得不等待回答我自己的帖子
  • 我还必须使用 Doctrine\Common\Persistence\ObjectManager
  • @gview 你可以添加依赖“use Doctrine\Common\Persistence\ObjectManager;”然后将该方法声明为“function load(ObjectManager $manager)”
  • 是的,这就是我所说的“使用 Doctrine\Common\Persistence\ObjectManager”
【解决方案2】:

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

They point this issue out here!

【讨论】:

    【解决方案3】:

    您应该添加 ObjectManager 依赖项:

    use Doctrine\Common\Persistence\ObjectManager;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多