too long

I have two entities namely, User and Role which has a many to many relationship between them. I generated a CRUD and it worked fine but the UserType form renderd a select menu for choosing roles for a user. Because I needed check boxes insted of a dropdown i made following changes to my UserType class

initial

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName')
            ->add('lastName')
            ->add('address')
            ->add('phone')
            ->add('nic')
            ->add('email')
            ->add('password')
            ->add('isActive')
            ->add('roles');
    }

after change

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName')
            ->add('lastName')
            ->add('address')
            ->add('phone')
            ->add('nic')
            ->add('email')
            ->add('password')
            ->add('isActive')
            ->add('roles',EntityType::class, array('class'=>'AppBundle\Entity\Role',
                'multiple'=>true,
                'expanded'=>true,));
    }

This is where it all started. I can create a new user without any problem but when I try to edit

Warning: spl_object_hash() expects parameter 1 to be object, string given

Error appears without rendering the edit form.

My attempts to resolve the issue

The user entity is used for login my system therefor I return an array of roles belongs to the user.

    public function getRoles()
        {
            $role_data = $this->roles->toArray();
            $roles = array();

            foreach($role_data as $key=>$role)
            {
                $roles[] = $role->getName();
            }

            return $roles;
        }

I changed the above getter to simply return an object as follows

    public function getRoles()
    {
        return $this->roles;
    } 

Now User Create and Edit worked fine but my login is broken since getRoles is no more returning an array

What I really want is to render the UserType form with checkboxes for selecting roles but when I try to do that this happened. I tried to find a solution without any success so far.

Form entity type expects list of objects (role entities) as its value, but gets list of strings as getRoles method returns list of strings. You can't change getRoles method as it is part of security component. You can rename roles field to something else and have 2 methods getXXX to get list of roles entities and getRoles for security component.

Ex.:

User

class User
{
    /**
     * @ORM...
     * ...
     */
    $sroles

    function getSroles()
    {
        return $this->sroles;
    }

    public function getRoles()
    {
        $role_data = $this->sroles->toArray();
        $roles = array();

        foreach($role_data as $key=>$role)
        {
            $roles[] = $role->getName();
        }

        return $roles;
    }
}

Form

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName')
            ->add('lastName')
            ->add('address')
            ->add('phone')
            ->add('nic')
            ->add('email')
            ->add('password')
            ->add('isActive')
            ->add('sroles',EntityType::class, array('class'=>'AppBundle\Entity\Role',
                'multiple'=>true,
                'expanded'=>true,));
    }