amitaibu / og

A fork to work on OG8
https://github.com/Gizra/og
29 stars 16 forks source link

Roles not saved on OgMembership entity when passing associative array #289

Open pfrenssen opened 7 years ago

pfrenssen commented 7 years ago

There is a bug in OgMembership::setRoles() that prevents the roles from being saved when an associative array of roles is passed in. Only when a numerically indexed array of roles is passed in the roles are correctly saved.

For example this currently fails:

  $membership = OgMembership::create();
  $membership
    ->setRoles(OgRole::loadMultiple($roles))
    ->save();

But this works:

  $membership = OgMembership::create();
  $membership
    ->setRoles(array_values(OgRole::loadMultiple($roles)))
    ->save();

The problem is somewhere around FieldItemList::filterEmptyItems() - I did not dig all the way through, but when an associative array is passed the roles are seen as empty.

We should put the call to array_values() in the setRoles() method.

It would also be a good idea to make a test that proves we can pass the result from OgRole::loadMultiple() to OgMembership::setRoles() since this will probably be very commonly used like this.

amitaibu commented 7 years ago

@nader77 care to check this one out?

amitaibu commented 7 years ago

(i.e. - fix the bug + tests)

nedSaf commented 7 years ago

@amitaibu Sure :)