Argument 1 passed to FOS\UserBundle\Model\Group::setRoles() must be of the type array,object given
Bonjour à vous,
J'utilise symfony2 depuis quelques mois et je me trouve face à une situation mentionné ci-dessus.
Au fait je souhaite en enregistrer un rôle pour un groupe.
Voici mes codes
Group
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| namespace FOS\UserBundle\Model;
abstract class Group implements GroupInterface
{
protected $id;
protected $name;
protected $roles;
public function __construct($name, $roles = array())
{
$this->name = $name;
$this->roles = $roles;
}
/**
* @param string $role
*
* @return Group
*/
public function addRole($role)
{
if (!$this->hasRole($role)) {
$this->roles[] = strtoupper($role);
}
return $this;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
/**
* @param string $role
*/
public function hasRole($role)
{
return in_array(strtoupper($role), $this->roles, true);
}
public function getRoles()
{
return $this->roles;
}
/**
* @param string $role
*
* @return Group
*/
public function removeRole($role)
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
/**
* @param string $name
*
* @return Group
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @param array $roles
*
* @return Group
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
} |
Role
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| /**
* @ORM\Entity
* @ORM\Table(name="ft_roles")
*/
class Role extends BaseRole
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\generatedValue(strategy="AUTO")
*/
protected $id;
protected $users;
/**
* @ORM\Column(type="string", length=255)
*/
protected $role;
/**
* @ORM\Column(type="string", length=255)
*/
protected $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param text $description
* @return Role
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return text
*/
public function getDescription()
{
return $this->description;
}
/**
* Constructor
*/
public function __construct()
{
// parent::__construct();
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add users
*
* @param \Cvm\UserBundle\Entity\User $users
* @return Role
*/
public function addUser(\Cvm\UserBundle\Entity\User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param \Cvm\UserBundle\Entity\User $users
*/
public function removeUser(\Cvm\UserBundle\Entity\User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
/**
* Set role
*
* @param string $role
* @return Role
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
public function __toString()
{
return (string) $this->role;
}
} |
formtype
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class GroupRoleFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, array('label' => 'form.group', 'translation_domain' => 'FOSUserBundle'))
->add( 'roles','entity',array(
'class' => 'Cvm\UserBundle\Entity\role',
'property' => 'role',
'multiple' => false,
'required' => false,
'label' => 'form.role',
'translation_domain' => 'FOSUserBundle'
));
}
public function setDefaultOptions(OptionsResolverInterface $options)
{
return array(
'data_class' => 'Cvm\UserBundle\Entity\Group',
);
}
public function getName()
{
return 'cvm_group_role';
}
} |
Controller
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public function addGroupRoleAction( Group $group ){
$em= $this->getDoctrine()->getManager();
$form= $this->createForm(new GroupRoleFormType,$group);
$request= $this->getRequest();
if( $request->isMethod('POST') ){
$form->bindRequest($request);
$role=array();
$role[]=$form->get('roles')->getData();
$group->setRoles($role);
if ($form->isValid()){
$em->persist($group);
$em->flush();
}
return $this->redirect($this->generateUrl('fos_user_group_list'));
}
return $this->render('UserBundle:Group:addrole.html.twig',array(
'form'=>$form->createView()
)) ;
} |
Quelqu'un pourrait-il m'aider svp ?
SOS please !