2
votes

Trying to create a form that will allow me to select User roles but it's not working.

Here is my User class:

namespace TestBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="TestBundle\Repository\UserRepository")
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

And here is my form definition:

class ChangeUserRolesType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $permissions = array(
             'ROLE_USER'        => 'First role',
             'ROLE_CONSULT'     => 'Second role',
             'ROLE_SUPER_ADMIN' => 'Third role'
         );

        $builder
            ->add('username', EntityType::class, array(
                'class' => 'TestBundle:User',
                'choice_label' => 'username'
            ))
            ->add(
                'roles',
                ChoiceType::class,
                array(
                    'label'   => 'Roles',
                    'choices' => $permissions,
                    'multiple' => true,
                    'expanded' => true
                )
            )
            ->add(
                'save',
                SubmitType::class
            );

    }


    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TestBundle\Entity\User',
        ));
    }

}

This is my controller action that creates the form:

/**
 * @Route("/")
 */
public function indexAction()
{
    $users = $this->getDoctrine()->getRepository('TestBundle:User')->findAll();

    $user = $this->getDoctrine()->getRepository('TestBundle:User')->findOneBy(array('username' => 'liviu'));

    $editUserForm = $this->createForm('TestBundle\Form\ChangeUserRolesType', $user);


    return $this->render('TestBundle:Default:index.html.twig', array(
        'users' => $users,
        'form' => $editUserForm->createView()
    ));
}

But I am getting this error:

Warning: Missing argument 1 for FOS\UserBundle\Model\User::hasRole(), called in /home/liviu/apps/app1/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 416 and defined 500 Internal Server Error - ContextErrorException

does anyone know what's going on ? I am on Symfony 3.0.*

2

2 Answers

1
votes

The PropertyAccess use hassers/issers if it can't find a getter.

Also, you should be able to fix this error by adding the following method in your User entity :

public function getRoles()
{
    parent::getRoles();
}

Note that you should add the following constructor :

public function __construct()
{
    parent::__construct();
}

Clear the cache, look for update your database schema and it should works.

0
votes

I did the exact same thing, but I used:

return $this->roles->toArray();

instead of

parent::getRoles(); 

because I can't logged in using the last one, but the problem persist when I try to edit my user in the sonata admin bundle.