Use the setter with a table

My problem is this: I am developing the security of my website. In my user registration form I have five fields: 1) Name 2 ) email 3) Password 4) isActive 5) role. Obviously I don't want the last two items visible in the form, because otherwise violate my security expectations.

Therefore, the item "isActive" I put one with the setter, but the role is an array and if I get the following: $user-> setRoles('ROLE_USER') is not working.

I got this error:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, string given, called in C:\xampp\htdocs\Lavoc\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 547 and defined in C:\xampp\htdocs\Lavoc\vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php line 47

Here is my code:

 public function registroAction()
{
    $peticion = $this->getRequest();
    $em = $this->getDoctrine()->getManager();
    $user = new User();
    $role = new Role();
    $role->addRole(array('ROLE_USER')); //it does not work
    $user->setRoles($role);  // User relate with Role
    $formulario = $this->createForm(new UserType(), $user);

    if ($peticion->getMethod() == 'POST')
    {
        $formulario->submit($peticion);

        if ($formulario->isValid()) {
        $encoder = $this->get('security.encoder_factory')->getEncoder($user);
        $user->setSalt(md5(time()));
        $passwordCodificado = $encoder->encodePassword($user->getPassword(), $user->getSalt() );
        $user->setPassword($passwordCodificado);

        $em = $this->getDoctrine()->getManager();

        $em->persist($user);
        $em->flush();
        return $this->redirect($this->generateUrl('usuario_registro'));
        }
    }

    return $this->render( 'ProyectoSeguridadBundle:Seguridad:registro.html.twig',
    array('formulario' => $formulario->createView())
    );
}

Here is my User model:

    <?php

namespace Proyecto\SeguridadBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Proyecto\SeguridadBundle\Entity\User
*
* @ORM\Table(name="lavoc_users")
* @ORM\Entity(repositoryClass="Proyecto\SeguridadBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @var string
    *
    * @ORM\Column(name="username", type="string", length=25, unique=true )
    */
    private $username;

    /**
    * @var string
    *
    * @ORM\Column(name="salt", type="string", length=32)
    */
    private $salt;

    /**
    * @var string
    *
    * @ORM\Column(name="password", type="string", length=64)
    */
    private $password;

    /**
    * @var string
    *
    * @ORM\Column(name="email", type="string", length=60, unique=true)
    */
    private $email;

    /**
    * @var boolean
    *
    * @ORM\Column(name="isActive", type="boolean")
    */
    private $isActive;

    /**
    * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
    *
    */
    private $roles;

    public function __construct()
    {
        $this->roles = new ArrayCollection();
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));

    }

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

    /**
    * Set username
    *
    * @param string $username
    * @return User
    */
    public function setUsername($username)
    {
        $this->username = $username;
        return $this;
    }

    /**
    * @inheritDoc
    */
    public function getUsername()
    {
        return $this->username;
    }

    /**
    * Set salt
    *
    * @param string $salt
    * @return User
    */
    public function setSalt($salt)
    {
        $this->salt = $salt;
        return $this;
    }

    /**
    * @inheritDoc
    */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
    * Set password
    *
    * @param string $password
    * @return User
    */
    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }

    /**
    * @inheritDoc
    */
    public function getPassword()
    {
        return $this->password;
    }

    /**
    * Set email
    *
    * @param string $email
    * @return User
    */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
    * Get email
    *
    * @return string
    */
    public function getEmail()
    {
        return $this->email;
    }

    /**
    * Set isActive
    *
    * @param boolean $isActive
    * @return User
    */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
        return $this;
    }

    /**
    * Get isActive
    *
    * @return boolean
    */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
    * @inheritDoc
    */
    public function getRoles()
    {

        $roles = array();
        foreach ($this->roles as $role) {
            $roles[] = $role->getRole();
        }

        return $roles;
    }

    /**
    * @inheritDoc
    */
    public function eraseCredentials() {
    }

    /**
    * @see \Serializable::serialize()
    */
    public function serialize() {
        return serialize(array($this->id,));
    }

    /**
    * @see \Serializable::unserialize()
    */
    public function unserialize($serialized) {
        list ($this->id,) = unserialize($serialized);
    }

    public function isAccountNonExpired() {
        return true;
    }

    public function isAccountNonLocked() {
        return true;
    }

    public function isCredentialsNonExpired() {
        return true;
    }

    public function isEnabled() {
        return $this->isActive;
    }

    /**
    * Set roles
    *
    * @param string $roles
    * @return User
    */
    public function setRoles($roles)
    {
        $this->roles = $roles;
        return $this;
    }

}

My Role.php

<?php
namespace Proyecto\SeguridadBundle\Entity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Role
*
* @ORM\Table(name="lavoc_roles")
* @ORM\Entity(repositoryClass="Proyecto\SeguridadBundle\Entity\RoleRepository")
*/
class Role implements RoleInterface
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;
    /**
    * @var string
    *
    * @ORM\Column(name="name", type="string", length=30)
    */
    private $name;
    /**
    * @var string
    *
    * @ORM\Column(name="role", type="string", length=20, unique=true)
    */
    private $role;
    /**
    * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
    */
    private $users;
    /**
    * Get id
    *
    * @return integer
    */
    public function getId()
    {
    return $this->id;
    }
    /**
    * Set name
    *
    * @param string $name
    * @return Role
    */
    public function setName($name)
    {
    $this->name = $name;
    return $this;
    }
    /**
    * Get name
    *
    * @return string
    */
    public function getName()
    {
    return $this->name;
    }
    /**
    * Set role
    *
    * @param string $role
    * @return Role
    */
    public function setRole($role)
    {
    $this->role = $role;
    return $this;
    }
    /**
    * @see RoleInterface
    */
    public function getRole()
    {
    return $this->role;
    }
    /**
    * Set users
    *
    * @param string $users
    * @return Role
    */
    public function setUsers($users)
    {
    $this->users = $users;
    return $this;
    }
    /**
    * Get users
    *
    * @return string
    */
    public function getUsers()
    {
    return $this->users;
    }
    public function __toString()
    {
    return $this->getName().' '.$this->getRole();
    }

    /**
     * Add roles
     *
     * @param \Seguridad\Entity\Role $roles
     */
    public function addRole(\MyBundle\Entity\Role $role) {
        $this->roles[] = $roles;
    }

    /**
     * Remove roles
     *
     * @param \SeguridadBundle\Entity\Role $roles
     */
    public function removeRole(\MyBundle\Entity\Role $role) {
        $this->roles->removeElement($roles);
    }

}


In this line $user->setRoles('ROLE_USER'), you try to add an string into a ArrayCollection. That's why you have an ArrayCollection exception.

Can you try to add this code (make sure to replace \MyBundle\Entity\Role by your own Role model)

/**
 * Add roles
 *
 * @param \MyBundle\Entity\Role $roles
 */
public function addRole(\MyBundle\Entity\Role $role) {
    $this->roles[] = $roles;
}

/**
 * Remove roles
 *
 * @param \MyBundle\Entity\Role $roles
 */
public function removeRole(\MyBundle\Entity\Role $role) {
    $this->roles->removeElement($roles);
}

/**
* @inheritDoc
*/
public function getRoles()
{
    return $this->roles->toArray();
}

To use it, you need to pass an Role object to the addRole or removeRole methods.

Other information

If you want define a default value for isActive and/or other values, you can just call the appropriate setter in the constructor of your User model.

Example:

public function __construct()
{
    // your own logic
    $this->isActive = true;
}

Hope it will help.