dongyanju0945 2017-07-29 06:25
浏览 43
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dongmubi4375 2017-07-29 07:55
    关注

    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,));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分