在FOS准则中使用服务容器

时间:2019-03-22 15:47:30

标签: php symfony fixtures

我正在使用DoctrineFixtureBundle来管理我的Web应用程序中的装置,我想使用默认图像来初始化媒体。

为此,我需要访问服务容器以获取RootDir参数。这是我的装置:


use Doctrine\Bundle\FixturesBundle\Fixture;

class AppFixtures extends Fixture
{
    private $passwordEncoder;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }
    public function load(ObjectManager $manager)
    {
        $root = new User();

        //init user parameters

        $image = new Image();
        $image->setImageFile(fopen($this->container->getRootDir().'public/image/default/no-profile-pic/jpg', 'r'));
        $root->setProfilePic($image);

        $manager->persist($root);
    }
}

我遵循了这个documentation,但它返回了以下错误:

 Notice: Undefined property: App\DataFixtures\AppFixtures::$container

我是愚蠢的还是AppFixture类不知道该容器?

1 个答案:

答案 0 :(得分:0)

访问完整的容器不再是解决方案。您需要使用DI来代替更改密码编码器的类定义