在将symfony 2.0升级到2.1 / 2.2后,容器依赖注入不能在Fixtures中工作 [英] Container Dependecy Injection not working in Fixtures after upgrading symfony 2.0 to 2.1/2.2

查看:112
本文介绍了在将symfony 2.0升级到2.1 / 2.2后,容器依赖注入不能在Fixtures中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



composer.json:

  {
name:symfony / framework-standard-edition,
license:MIT,
type:project,
description \\Symfony Standard Edition \发行版
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 。*,
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
kriswallsmith / assetic:1.1.*@dev,
knplabs / knp-menu-bundle:dev 主人,
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\\Di stributionBundle\\\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
}
}

}



,我的装置看起来像这样: p>

LoadUserData.php

 <?php 
命名空间FINDOLOGIC \CustomerLoginBundle\DataFixtures\ORM;

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


class LoadUserData实现FixtureInterface,ContainerAwareInterface
{
/ **
* @var ContainerInterface
* /
private $容器;

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

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

我已经看过但是,它不能解决我的问题。



我真的非常期待您的投入,因为我一直在努力解决这一段时间。

解决方案

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

  public function testLogin()
{

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

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

现在可以正常工作。


I upgraded a project from Symfony 2.0 to 2.1. After that my tests stopped working. The problem is that I use fixtures which implement the ContainerAware interface. Now after the upgrade the setContainer() method does not get called anymore. I tried upgrading further to 2.2. The problem still persists.

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"
    }
}

}

and my fixture looks like this:

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)
    {
     [...]
    }
}

I've already taken a look at Doctrine fixtures not working after updating to Symfony 2.1.8 but it does not solve my problem.

I'm really looking forward to your input as I have been trying to solve this for quite a while now.

解决方案

I had exactly the same issue, nothing would fix it via changing round composer order, order in the kernal, extending or implementing etc, so in the end, I just pass the container in from the test:

public function testLogin()
{

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

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

And now it works fine.

这篇关于在将symfony 2.0升级到2.1 / 2.2后,容器依赖注入不能在Fixtures中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆