Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2将表单数据发送到数据库。什么也没发生_Php_Symfony - Fatal编程技术网

Php Symfony2将表单数据发送到数据库。什么也没发生

Php Symfony2将表单数据发送到数据库。什么也没发生,php,symfony,Php,Symfony,这看起来很简单。我是通过使用文档来做到这一点的,但是我只是没有创建一个新用户。我没有得到任何错误,但我也没有得到用户。我在一个单独的类中创建表单,如下所示: class RegisterFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder

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

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)
,它应该可以工作。因为您正在创建表单,但没有处理来自请求的数据。如果你愿意的话,你可以看一下

如果什么也没有发生,那么您的表单可能无效。但是我成功地收到了post 200请求,这是因为您没有发送任何其他HTTP状态代码。可能存在重复的
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(),    
           ));  
}