使用PUGX多用户将用户传递给symfony2 formType

时间:2014-12-02 11:09:12

标签: symfony fosuserbundle pugxmultiuserbundle

我正在使用PUGX MultiuserBundle,因为我有3种不同类型的用户,具有不同的信息。

一切运作良好,我可以注册那些不同的用户。

我想做的是:注册用户一个(没问题)=>这个用户可以创建他的伙伴并选择他给他的建立。

所以,在我的第二个formType中,我必须通过用户一个,所以我可以得到他所有的建立。但我不知道如何使用PUGX捆绑包

我的控制器:

class BarmanController extends Controller
{
    /**
     * Register new Barman entities.
     *
     * @Route("/register", name="barman_register")
     * @Template()
     */
    public function registerAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        return $this->container
                    ->get('pugx_multi_user.registration_manager')
                    ->register('Cac\UserBundle\Entity\Barman', $user);
    }
}

PUGX RegistrationManager:

/**
     * 
     * @param string $class
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function register($class, $user)
    {
        $this->userDiscriminator->setClass($class);
        $this->controller->setContainer($this->container);
        $result = $this->controller->registerAction($this->container->get('request'));        
        if ($result instanceof RedirectResponse) {
            return $result;
        }

        $template = $this->userDiscriminator->getTemplate('registration');
        if (is_null($template)) {
            $engine = $this->container->getParameter('fos_user.template.engine');
            $template = 'FOSUserBundle:Registration:register.html.'.$engine;
        }

        $form = $this->formFactory->createForm();   
        return $this->container->get('templating')->renderResponse($template, array(
            'form' => $form->createView(),
        ));
    }

我的第二个用户的formType:

class BarmanFormType extends AbstractType
{
    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $id = 5;
        $builder
            ->add('username', null, array('label' => 'Nom d\'utilisateur'))
            ->add('plainPassword', 'repeated', array(
                'label' => 'Mot de passe',
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle'),
                'first_options' => array('label' => 'Mot de passe'),
                'second_options' => array('label' => 'Confirmez votre mot de passe'),
                'invalid_message' => 'fos_user.password.mismatch',
            ))
            ->add('email', 'repeated', array(
                    'type' => 'email',
                    'options' => array('translation_domain' => 'FOSUserBundle'),
                    'first_options' => array('label' => 'E-mail'),
                    'second_options' => array('label' => 'Confirmez votre e-mail'),
                    'invalid_message' => 'fos_user.email.mismatch',
            ))
            ->add('bar', 'entity', array('class' => 'CacBarBundle:Bar', 'property' => 'name', 'attr' => array('placeholder' => 'Bar'),
                'query_builder' => function(\Cac\BarBundle\Entity\BarRepository $er) use ($id) {
                    return $er->getBarByAuthorId($id);
                }
            ));
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

    public function getName()
    {
        return 'cac_barman_registration';
    }
}

我还有一个覆盖FOS用户注册控制器的通用寄存器控制器:

class RegistrationController extends Controller
{
    public function registerAction(Request $request)
    {
        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('fos_user.registration.form.factory');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->get('event_dispatcher');

        $user = $userManager->createUser();
        $user->setEnabled(true);

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }

        $form = $formFactory->createForm();

        $form->setData($user);

        $form->handleRequest($request);

        if ($form->isValid()) {
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $userManager->updateUser($user);

            if (null === $response = $event->getResponse()) {
                if (strpos($request->getPathInfo(), "/barman/") === false) {
                    $url = $this->generateUrl('fos_user_registration_confirmed');
                    $response = new RedirectResponse($url);
                }
                else
                {
                    $this->get('session')->getFlashBag()->add('notice','Le barman a était créé !');
                    $response = new Response('barman');
                }
            }

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }

        return $this->render('CacUserBundle:Registration:register.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

我设法让用户进入PUGX部分,但我不知道将它从RegistrationManager传递给我的FormType

我尝试了很多可能性,我认为我已经接近但无法找到解决方案让它发挥作用

0 个答案:

没有答案