在FOS DoctrineFixture中使用服务容器



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

要这样做,我需要访问服务容器以获取RootDir参数。这是我的固定装置:


use DoctrineBundleFixturesBundleFixture;
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);
    }
}

我遵循此文档,但它返回了以下错误:

 Notice: Undefined property: AppDataFixturesAppFixtures::$container

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

访问完整容器不再是解决方案。您需要通过更改类定义来使用DI,例如密码编码器

最新更新