duanpo2813
duanpo2813
2017-11-23 09:36

如何在hautelook alicebundle中设置角色datafixtures?

已采纳

I'm having a hard time to fix this problem by adding roles in datafixtures using hautelook bundle. I create a provider that will add a roles to the fixtures

<?php

namespace AdminBundle\DataFixtures\Faker\Provider;
use AppBundle\Entity\AdminUserTypes;

class Roles
{
    public function roleAdvocate(){

        return ['ROLE_ADMIN', 'ROLE_ADVOCATE'];
    }
}

in my datafixtures yml i have this code

AppBundle\Entity\AdminUser:
    adminuser_101:
        username: '<username()>'
        email: '<email()>'
        adminUserType: 2
        enabled: true
        roles: '<roleAdvocate()>'

when the time the I run the phpunit, i always get this result error from roles

  Warning: in_array() expects parameter 2 to be array, null given

I have no idea now on how to fix it. Even I tweak the array. I get an error. I have this link but it doesn't work either. same result

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanfengwang9157 duanfengwang9157 4年前

    In my project. I create another Entity called AdminUser Entity. This entity I inherit the abstract class User

    class AdminUser extends User
    

    Then I realize I forgot to add this method to inherit the parents constructor.

     public function __construct()
        {
            parent::__construct();
        }
    
    点赞 评论 复制链接分享
  • douchun1900 douchun1900 3年前

    Syntax for arrays with AliceBundle uses []

    So this should work:

    AppBundle\Entity\AdminUser: adminuser_101: username: '<username()>' email: '<email()>' adminUserType: 2 enabled: true roles: ['<roleAdvocate()>']

    点赞 评论 复制链接分享
  • duangang4940 duangang4940 3年前

    You can simply define $roles like that in your AdminUser entity :

    /**
     * @var array
     */
    protected $roles = [];
    

    By default $roles is null and can't be used as an array.

    点赞 评论 复制链接分享