The easiest solution is to extend Symfony\Component\DependencyInjection\ContainerAware.
<?php
namespace SomeCompany\SomeBundle\DataFixtures\MongoDB;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerInterface;
use SomeCompany\SomeBundle\Document\User;
class LoadUsers extends ContainerAware implements FixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->createUser();
$user->setUsername('myuser');
$user->setEmail('a@a.com');
$user->setPlainPassword('mypass');
$user->addRole('ROLE_USER');
$user->setEnabled(true);
$userManager->updateUser($user);
}
}
If you are already extending another class, then you have to implement the ContainerAwareInterface, which takes just a few extra lines of code.