Skip to content

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

@pfrenssen

Description

@pfrenssen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions