バージョン: Symfony 2.2
ユーザーが自分の Web サイトに登録するときに、デフォルトの役割を追加しようとしています。私は FOSUserBundle を使用していますが、ユーザーがデータベースでロール フィールドを登録すると、空になっていることがわかります。この巨大なバンドルから始めますが、理解するのは簡単ではありません。だから私はすべてのドキュメントを読みましたが、何をすべきかわかりません。
今のところ、このロールを動的に追加するイベントを作成しますが、機能しません (エラーはありませんが、データベースはまだ空です)。
私のイベント:
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AddDefaultRoleListener implements EventSubscriberInterface {
private $container;
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onAddDefaultRoleSuccess',
);
}
public function onAddDefaultRoleSuccess(FormEvent $event)
{
$doctrine = $this->container->get('doctrine');
$em = $doctrine->getManager();
$user = $event->getForm()->getData();
$user->addRole('ROLE_USER');
//$user->setRoles(array('ROLE_USER'));
$em->persist($user);
}
}
ご覧のとおり、REGISTRATION_SUCCESS をリッスンする単純なイベントを作成しましたが、何も機能していないようです。イベントとサービスでの私の最初の試みです。だから誰かがアドバイスを持っているなら、私はそれを受け入れます:)