1

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

mega6382
  • 9,211
  • 17
  • 48
  • 69
user3818576
  • 2,979
  • 8
  • 37
  • 62

3 Answers3

1

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.

Pierre M
  • 11
  • 1
  • 1
1

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()>']

Roubi
  • 1,989
  • 1
  • 27
  • 36
-1

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();
    }
user3818576
  • 2,979
  • 8
  • 37
  • 62