i have one to many relationship between my user and roles. while creating a new user i have render all the roles from roles entity and render it on with create user form. after putiting user info and selecting roles. when inserting into the db it says
Warning: in_array() expects parameter 2 to be array, object given
Below is my code.
User Entity
/**
* @ORM\OneToMany(targetEntity="PNC\PermissionsBundle\Entity\Roles", mappedBy="users", cascade={"persist"})
* @var ArrayCollection $role
*/
private $role;
/**
* @return ArrayCollection A Doctrine ArrayCollection
*/
public function getRole()
{
return $this->role;
}
/**
* @param mixed $role
*/
public function setRole(ArrayCollection $role)
{
foreach($role as $rol) {
$rol->setUsers($this);
}
$this->role = $role;
}
public function __construct() {
parent::__construct();
$this->roles = new ArrayCollection();
}
Roles Entity:
/**
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="role")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $users;
/**
* Set Users
*
* @param \Doctrine\Common\Collections\ArrayCollection $users
* @return Roles
*/
public function setUsers(ArrayCollection $users)
{
$this->users = $users;
return $this;
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getUsers()
{
return $this->users;
}
and the form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstname','text',array(
'label' => ucfirst('First Name *'),
'attr'=> array(
'class' => 'form-control',
'autocomplete' => 'off'
)))
->add('role', 'entity', array(
'class' => 'PNCPermissionsBundle:Roles',
'property' => 'role',
//'choice_list' => new SimpleChoiceList($optionArray),
'multiple' => true,
'expanded' => true,
'attr'=> array(
'class' => 'minimal',
'style' => 'position: absolute; opacity: 0;'
)
))
......
->add('save', 'submit', array(
'attr' => array('class' => 'submit'),
'label' => "Save"
))
;
}
and the controller:
public function newAction(Request $request){
$basePath = $request->getScheme().'://'.$request->getHost().$request->getBaseUrl();
$user = new User();
$form = $this->createForm(new UsersType(), $user, array(
'action' => $this->generateUrl('pnc_permissions_user_add'),
'method' => 'POST',
));
if ($request->getMethod() == 'POST')
{
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
$password = $encoder->encodePassword( $user->getPassword() , $user->getSalt());
$user->setPassword($password);
$em->persist($user);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'New user created successfully.');
return $this->redirect($this->generateUrl('pnc_permissions_user_index'));
}
}
return $this->render('PNCPermissionsBundle:Users:add_user.html.twig', array(
'userForm' =>$form->createView(),
));
}
New to symofny, unable to figure out the error. Plus i am using fos user bundle also. and on error page it says this;
Stack Trace
in vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Model/User.php at line 142 -
return $this;
}
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}