将symfony 2.0升级到2.1 / 2.2后,容器依赖注入在Fixtures中不起作用

时间:2013-05-28 10:28:13

标签: php symfony doctrine symfony-2.1 symfony-2.2

我将项目从Symfony 2.0升级到2.1。之后我的测试停止了工作。问题是我使用实现ContainerAware接口的fixture。现在升级后,setContainer()方法不再被调用。我尝试进一步升级到2.2。问题仍然存在。

composer.json:

{
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
    "psr-0": { "": "src/" }
},
"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.2.*",
    "doctrine/orm": ">=2.2.3,<2.5-dev",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "1.0.*@dev",
    "symfony/assetic-bundle": "2.1.*",
    "symfony/swiftmailer-bundle": "2.2.*",
    "symfony/monolog-bundle": "2.2.*",
    "sensio/distribution-bundle": "2.2.*",
    "sensio/framework-extra-bundle": "2.2.*",
    "sensio/generator-bundle": "2.2.*",
    "jms/security-extra-bundle": "1.4.*",
    "jms/di-extra-bundle": "1.3.*",
    "kriswallsmith/assetic": "1.1.*@dev",
"knplabs/knp-menu-bundle":"dev-master",
"knplabs/knp-menu":"dev-master",
    "doctrine/doctrine-fixtures-bundle": "dev-master",
    "doctrine/data-fixtures": "1.0.*@ALPHA",
"doctrine/migrations": "dev-master",
"doctrine/doctrine-migrations-bundle": "dev-master",
    "jms/translation-bundle": "dev-master"


},
"scripts": {
    "post-install-cmd": [
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
    ],
    "post-update-cmd": [
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
    ]
},
"extra": {
    "symfony-app-dir": "app",
    "symfony-web-dir": "web",
    "branch-alias": {
        "dev-master": "2.2-dev"
    }
}

}

我的夹具看起来像这样:

LoadUserData.php

<?php
namespace FINDOLOGIC\CustomerLoginBundle\DataFixtures\ORM;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load(ObjectManager $manager)
    {
     [...]
    }
}

我已经看过Doctrine fixtures not working after updating to Symfony 2.1.8,但它并没有解决我的问题。

我真的很期待你的意见,因为我一直试图解决这个问题。

2 个答案:

答案 0 :(得分:2)

我有完全相同的问题,没有什么可以通过改变圆形作曲家顺序,在内核中的顺序,扩展或实现等来解决它,所以最后,我只是从测试中传递容器:

public function testLogin()
{

///
$fixture = new UserFixtures();
$fixture->setContainer($container);
///

$executor->execute($loader->getFixtures(), true);

现在它运作正常。

答案 1 :(得分:-2)

<强>解决方案:

确保您不仅实现ContainerAwareInterface - 您应扩展ContainerAware

use Symfony\Component\DependencyInjection\ContainerAware;

// ContainerAware... already implements ContainerAwareInterface

class YourFixture extends ContainerAware 
{
    // ...

另一个建议

在composer.json中(在doctrine / orm之前)进一步向上移动Datafixtures,让作曲家正确生成类图。

2.0 documentation州。

  

确保在Doctrine \ Common之前注册新的命名空间。   否则,Symfony将在其中查找数据夹具类   Doctrine \ Common目录。 Symfony的自动加载器总是寻找一个   class在第一个匹配命名空间的目录中,所以更多   应始终首先使用特定的名称空间。

此外,你不需要灯具和灯具,因为灯具是灯具束的依赖。

希望有所帮助。