Search code examples
phpsymfonysonata-adminsonata-user-bundle

Symfony 2, Sonata User Bundle : Invalid state, originalRoles array is not set


When i try to create a new user, with or without role(s) selected i straight have this error :

Invalid state, originalRoles array is not set

Roles choices are added that way (Not modified, straight from vendor):

->add('realRoles', 'sonata_security_roles', array(
                    'label'    => 'form.label_roles',
                    'expanded' => true,
                    'multiple' => true,
                    'required' => false
                ))

And here the code that return the exception (straight from vendor, so probably not a good idea to fix the error here except if it is a real issue).

/**
 * {@inheritdoc}
 */
public function reverseTransform($selectedRoles)
{
    if ($this->originalRoles === null) {
        throw new \RuntimeException('Invalid state, originalRoles array is not set');
    }

    list($availableRoles, ) = $this->rolesBuilder->getRoles();

    $hiddenRoles = array_diff($this->originalRoles, $availableRoles);

    return array_merge($selectedRoles, $hiddenRoles);
}

Any solutions or hints?

UPDATE :

Issue moved to Github Sonata with some more information.


Solution

  • Problem caused by redefining of constructor in entity extending from Sonata\UserBundle\Entity\BaseUser

    To solve this problem, add parent::construct() in the child entity.