0

我的用户和角色之间有一对多的关系。在创建新用户时,我已经渲染了角色实体中的所有角色并使用创建用户表单渲染它。在输入用户信息并选择角色之后。插入数据库时​​,它说 Warning: in_array() expects parameter 2 to be array, object given

下面是我的代码。

用户实体

    /**
     * @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();

    }

角色实体:

  /**
     * @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;
    }

和形式:

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"
        ))
        ;
    }

和控制器:

 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(),
        ));
    }

symofny 的新手,无法找出错误。另外,我也在使用 fos 用户捆绑包。并在错误页面上这样说;

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;
        }
4

1 回答 1

3

改用这个:

if (!$this->roles>contains($role)) {
        $this->roles[] = $role;
    }

in_array 用于数组,包含用于ArrayCollection

于 2016-05-12T09:04:39.660 回答