2
votes

This should be so simple, but I'm having one of those days.

(I'm using Symfony 2.4.1)

I've followed Using Groups With FOSUserBundle to the letter. I want to allow the user to select a group(s) when registering. So as I've followed, I've created the Many To Many relationship and have a fos_user_user_group table to contain the relationships.

I've also followed Overriding Default FOSUserBundle Forms so that I can add the groups field in the registration form to be selected.

Following the above (not adding my own setGroups function etc), I get the following error

ContextErrorException: Warning: array_merge(): Argument #2 is not an array in C:\Users\ppounder\Documents\Development\KP\htdocs\symfony\kp\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Model\User.php line 279

in C:\Users\ppounder\Documents\Development\KP\htdocs\symfony\kp\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Model\User.php line 279 at ErrorHandler->handle('2', 'array_merge(): Argument #2 is not an array', 'C:\Users\ppounder\Documents\Development\KP\htdocs\symfony\kp\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Model\User.php', '279', array('roles' => array(), 'group' => object(Group)))

Looking at the tables, everything has gone in as I would expect. The user is added, the groups and users are listed in the join table.

I have no getters and setters in my User entity for my groups as the FOSUserBundle doesn't mention any need. However I've tried adding the following into my construct in my User entity:

public function __construct()
{
    parent::__construct();
    // your own logic
    $this->groups = new ArrayCollection();
}

But still no luck. As the error above shows, the second argument is an object.

FYI - My FormType buildForm method for the groups field is this:

$builder->add('groups', 'entity', array(
    'class' => 'AcmeDemoBundle:Group',
    'property' => 'name',
    'expanded' => true,
    'multiple' => true,
    'required' => true,
    'empty_value' => '--Select Group--'
));

Any help would be appreciated.

UPDATE

This error also occurs when I try logging in and a user is assigned to a group or more. Therefore it must be something to do with the getGroups() function (not in my User entity). I must have to override, but how?

If I add the following, it removes the error, but stops inserting into the join table

public function getGroups()
{
    return $this->groups = new ArrayCollection;
}
1

1 Answers

3
votes

Found it, and it was nothing to do with the getGroup and setGroup.

The error was when the user roles and group roles were merging. When I set up a group for some reason it wasn't storing the contents as an array in my Group table. Therefore it couldn't array_merge them together.

Sorted the roles field out in the Group table and it now works.

Hope that helps someone in the future.