10

I'm trying to create a new User Admin from a fixture. I'm using FOSUserBundle and Symfony2.

$userManager = $this->container->get('fos_user.user_manager');

//$userAdmin = $userManager->createUser();

$userAdmin = new UserAdmin();
$userAdmin->setUsername('francis');
$userAdmin->setEmail('francis@francis.com');
$userAdmin->setEnabled(true);
$userAdmin->setRoles(array('ROLE_ADMIN'));

$userManager->updateUser($userAdmin, true);

I'm always getting this error:

[ErrorException]                                         
Notice: Undefined property:     
INCES\ComedorBundle\DataFixtures\ORM\LoadUserAdminData::$container in 
/public_html/Symfony/src/INCES/ComedorBundle/DataFixtures/ORM/LoadUserAdminData.php line 16  
Anil
  • 21,730
  • 9
  • 73
  • 100
Gabriel Muñumel
  • 1,876
  • 6
  • 34
  • 57

5 Answers5

27

This worked for me (i'm also using FOSUserBundle):

// Change the namespace!
namespace Acme\DemoBundle\DataFixtures\ORM;

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

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    //.. $container declaration & setter

    public function load(ObjectManager $manager)
    {
        // Get our userManager, you must implement `ContainerAwareInterface`
        $userManager = $this->container->get('fos_user.user_manager');

        // Create our user and set details
        $user = $userManager->createUser();
        $user->setUsername('username');
        $user->setEmail('email@domain.com');
        $user->setPlainPassword('password');
        //$user->setPassword('3NCRYPT3D-V3R51ON');
        $user->setEnabled(true);
        $user->setRoles(array('ROLE_ADMIN'));

        // Update the user
        $userManager->updateUser($user, true);
    }
}

Hope this helps someone! :)

Anil
  • 21,730
  • 9
  • 73
  • 100
  • Cool its work for me too :) Tnx. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/user_manager.md - This is the userManager documentation if someone want to do something diffrent :) – Petroff Jul 03 '14 at 11:19
  • This works but how can we load a user from database if we are going to make an '--append' loading and we dont wana create a new user but use an existing one. – Salim Ben Aissa Mar 15 '19 at 19:18
3

Follow this section of the documentation.

Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
2

The Error is because the $container is currently undefined. To fix this, add the ContainerAwareInterface to your class definition.

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    ...
}

This will not completely get you what you want though, since you are creating the user without the UserManager. Instead you should be using the line you have commented out.

It seems to me that you don't need the UserAdmin class. The admin users should be a subset of the User distinguished only by the roles that they have.

You should use the UserManager to create a User(not UserAdmin) and set the roles. If you need to keep an index of all admin users, a MySQL VIEW could accomplish this, or you could create your own custom "cache" table and use Doctrine Listeners to update it when needed.

This question is fairly old, so I am guessing you found the answer or at least a workaround. Would you please provide that? It is ok to answer your own questions.

cmgriffing
  • 117
  • 2
  • 12
0

This is the updated version for SF3+ This answer is based on Anil and Mun Mun Das answers.

namespace App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use FOS\UserBundle\Model\UserManagerInterface;

    class AdminFixtures extends Fixture
    {

        private $userManager;

        public function __construct(UserManagerInterface $userManager)
        {
            $this->userManager = $userManager;
        }

        public function load(ObjectManager $manager)
        {
            $user = $this->userManager->createUser();
            $user->setUsername('username');
            $user->setEmail('email@domain.com');
            $user->setPlainPassword('password');
            $user->setEnabled(true);
            $user->setRoles(array('ROLE_ADMIN'));
            $this->userManager->updateUser($user);
        }
    }
Kaizoku Gambare
  • 3,143
  • 3
  • 29
  • 41
0

This is what I did using Symfony 4, SonataAdmin and FosUserBundle

namespace App\DataFixtures;

use App\Application\Sonata\UserBundle\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class UserFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setUsername('yourusername');
        $user->setEmail('youremail@email.com');
        $user->setEnabled(true);
        $user->setPlainPassword('yourpassword');
        $user->setRoles(array('ROLE_SUPER_ADMIN'));
        $manager->persist($user);

        $manager->flush();
    }
}
laviku
  • 531
  • 12
  • 32