通过控制器 FosUserBundle 创建用户

CreateUser through controller FosUserBundle

我将尝试向数据库中添加一些数据,并在同一个控件中通过 userManager 创建用户。我会做这样的事情,当我提交这个我渲染页面的表单时没有任何错误,只是重新加载,这甚至没有重定向到路由,什么都没有。 知道出了什么问题吗?

已编辑

我的控制器现在看起来像:

/**
     * @Route("/dystrybutor/pracownicy/add", name="dystrybutor_pracownicy_add")
     */

    public function new(Request $request)
    {


    $pracownik = new Pracownik();

    $form = $this->createForm(PracownikType::class, $pracownik);

        return $this->render('dystrybutor_pracownicy/new.html.twig', array(
            'form' => $form->createView(),
        ));



        $form->handleRequest($request);





    if ($form->isSubmitted() && $form->isValid()) {
        $id = $this->getUser()->getDystrybutorId();

        $username = $form["username"]->getData();
        $password = $form["password"]->getData();
        $email = $form["email"]->getData();



        $userManager = $container->get('fos_user.user_manager');
        $user = $userManager->createUser();
        $user->setUsername($username);
        $user->setEmail($email);
        $user->setLocked(0); 
        $user->setEnabled(1); 
        $user->setPlainPassword($password);
        $user->addRole("ROLE_PRACOWNIK");



                try {


            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

        } catch (\PDOException $e) {

            $this->get('session')->getFlashBag()->add(
                'error', 'Wystąpił błąd przy udodawaniu użytkownika!'
            );
            return $this->redirect($this->generateUrl('dystrybutor_pracownicy'));

        }  


        return $this->redirectToRoute('dystrybutor_pracownicy');
    }
    }

但是 ORM 仍然没有将用户添加到数据库,没有任何反应。这是我在这个控制器中使用的:

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Entity\Dystrybutor;
use App\Entity\Clients;
use App\Entity\Pracownik;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Security\Core\User\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use App\Form\PracownikType;
use FOS\UserBundle\Doctrine\UserManager;

这就是我构建自己的 formType 的方式,我确信问题是我没有正确提交它并且控制器没有处理它。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username')
        ->add('name')
        ->add('surname')
        ->add('phone')
        ->add('password', RepeatedType::class, array(
                'type' => PasswordType::class,
                'invalid_message' => 'Hasła muszą być takie same.',
                'options' => array('attr' => array('class' => 'password-field')),
                'required' => true,
                'first_options'  => array('label' => 'Hasło'),
                'second_options' => array('label' => 'Powtórz hasło'),
            ))
        ->add('email')
        ->add('save', SubmitType::class, array('label' => 'Dodaj pracownika'));
}
/**
 * @Route("/user/new", name="new_user")
 */
public function addUser(UserManagerInterface $userManager, EntityManagerInterface $entityManager) // use dependency injection as recommended by symfony
{
    $user = $userManager->createUser();

    $user->setPassword('1312adaSz');
    $user->setEmail('eadas@emadi.com');
    $user->setUsername('user6');
    $user->setEnabled(true);
    $user->setRoles(array_merge($user->getRoles(), array_diff(['ROLE_USER', 'ROLE_ADMIN'], $user->getRoles()))); // roles stored as array

    $entityManager->persist($user); // add user to queue
    $entityManager->flush();