Symfony2向数据库发送表单数据.什么也不会发生


Symfony2 sending form data to the database. Nothing happens

这看起来是个简单的任务。我通过使用文档来做到这一点,但是我只是没有创建一个新用户。我没有得到任何错误,但我也没有得到用户。我在一个单独的类中创建我的表单,像这样:

class RegisterFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options) {
                $builder                
                ->add('firstname', 'text', array(
                'label' => 'First Name * ',
                'attr' => array('placeholder' => 'First Name')))
                ->add('email', 'email', array(
                'label' => 'Email * ',
                'attr' => array('placeholder' => 'Email')))
                ->add('password', 'password', array(
                'label' => 'Password * ',
                'attr' => array('placeholder' => 'Password')))
                ->add('save', 'submit', array('label' => 'Register'));

    }
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Mp'ShopBundle'Entity'Users',
    ));
}
public function getName()
    {
        return 'register_form_users';
    }
}

在我的第二个控制器中,我获得数据库,创建一个新用户并向该用户添加信息?至少我认为我是……这里出了什么问题?

public function registerAction(Request $request)
    {   
            $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
            $user = new Users();
            $form = $this->createForm(new RegisterFormType(), $user);

            if ($form->isValid()) {
                $firstname = $form->get('firstname')->getData();
                $user->setFirstname($firstname);
                $email = $form->get('email')->getData();
                $user->setEmail($email);
                $password = $form->get('password')->getData();
                $user->setPassword($password);
                $em->persist($user);
                $em->flush();
            }
                return $this->render('MpShopBundle:Frontend:registration.html.twig',  array(
               'products'=>$products,
               'form'=>$form->createView(),    
               ));  
    }

$form->isValid()之前添加$form->handleRequest($request),应该可以工作。因为您正在创建表单,但没有处理来自请求的数据。如果你想的话,你可以看看源代码。

public function registerAction(Request $request)
{   
        $em = $this->getDoctrine()->getManager();
        $products = $em->getRepository('MpShopBundle:Product')->findAll();
        $user = new Users();
        $form = $this->createForm(new RegisterFormType(), $user);
        /** add this code */
        $form->handleRequest($request);
        /** add this code */

        if ($form->isValid()) {
            $firstname = $form->get('firstname')->getData();
            $user->setFirstname($firstname);
            $email = $form->get('email')->getData();
            $user->setEmail($email);
            $password = $form->get('password')->getData();
            $user->setPassword($password);
            $em->persist($user);
            $em->flush();
        }
            return $this->render('MpShopBundle:Frontend:registration.html.twig',  array(
           'products'=>$products,
           'form'=>$form->createView(),    
           ));  
}