表单不将数据发送到数据库

时间:2018-10-11 06:42:23

标签: symfony

我是Symfony的新手,我已经阅读了几乎所有有关将数据发送到数据库的文章。我已包含所有答案/解决方案,但仍未发送到数据库。 有人可以帮忙吗?

  

我已经创建了一个表单,我想发送数据库中的所有值,并且还创建了一个类为“ Profile”的实体

public function createProfile(Request $request)
{

        $em = $this->getDoctrine()->getManager();
        $profile = new Profile();

        $profile->setUserName('Name');
        $profile->setUserPosition('Position');
        $profile->setUserPhone('Phone');   
        $profile->setUserEmail('E-mail');   
        $profile->setUserDepartment('Department');



    $form = $this->createFormBuilder($profile)
        ->add('userName', TextType::class)
        ->add('userDepartment', ChoiceType::class, array(
             'placeholder' => 'Select department',
                'choices' => array(
                    'HR' => 'hr',
                    'Marketing' => 'marketing',
                    'Development' => 'development',
                    'Finance'   => 'finance',
                    'Logistics' => 'logistics',
                ),
        )) 
        ->add('userPosition', TextType::class)
        ->add('userPhone', TextType::class)
        ->add('userEmail', TextType::class)
        ->add('save', SubmitType::class, array('label' => 'Create Profile'))
        ->getForm();


     $form->handleRequest($request);

     // the if statement does not work :(
     if ($form->isSubmitted() && $form->isValid())  {

        dump($profile); // this doesn't return
        $form->bind($request);

        $form['userName']->getData();
        $form['userDepartment']->getData();
        $form['userPosition']->getData();
        $form['userPhone']->getData();
        $form['userEmail']->getData();

        $em->persist($profile);
        $em->flush();

    }

    return $this->render('dashboard/new_profile.html.twig', array(
        'form' => $form->createView(),
    ));
}

// new_profile.html.twig

{{ form_start(form) }}
{{ form_label(form.userName, 'Name') }}

{{ form_widget(form.userName, {'attr': {'placeholder': 'Your name', 'value':''} }) }}

{{ form_label(form.userPosition, 'Position') }}
{{ form_widget(form.userPosition, {'attr': {'placeholder': 'Your position', 'value':''} }) }}

{{ form_label(form.userPhone, 'Phone') }}
{{ form_widget(form.userPhone, {'attr': {'placeholder': 'Your phone', 'value':''} }) }}

{{ form_label(form.userEmail, 'E-mail') }}
{{ form_widget(form.userEmail, {'attr': {'placeholder': 'Your e-mail', 'value':''} }) }}

{{ form_label(form.userDepartment, 'Department') }}
{{ form_widget(form.userDepartment, {'attr': {'placeholder': 'Your department', 'value':''} }) }}

{{ form_rest(form) }}
{{ form_end(form) }}

1 个答案:

答案 0 :(得分:0)

首先,您应该分拆代码。您是控制者文件,应该控制会发生什么,而不是构建所有内容。

Src/Form目录下构建表单。创建一个名为Profile的新文件夹,并在名为AddType.php的文件下创建一个文件-完整路径应为Src/Form/Profile/AddType.php

# Src/Form/Profile/AddType.php

<?php
    namespace App\Form\Profile;

    use App\Entity\Profile;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\CheckType;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\Form\Extension\Core\Type\TextType;

    class AddType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('userName', TextType::class)
                ->add('userDepartment', ChoiceType::class, array(
                    'placeholder' => 'Select department',
                    'choices' => array(
                        'HR' => 'hr',
                        'Marketing' => 'marketing',
                        'Development' => 'development',
                        'Finance'   => 'finance',
                        'Logistics' => 'logistics',
                    ),
                ))
                ->add('userPosition', TextType::class)
                ->add('userPhone', TextType::class)
                ->add('userEmail', TextType::class)
                ->add('save', SubmitType::class, array('label' => 'Create Profile'));
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => Profile::class
            ));
        }
    }

然后在您的控制器中:

<?php
    class CreateController extends Controller
    {
        public function index(Request $request)
        {
            $profile = new Profile();

            $form = $this->createForm(AddType::class, $profile);
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getManager();

                $em->persist($profile);
                $em->flush();
                # what other actions you need. e.g redirect to success page
            }

            return $this->render('product/add.html.twig', ['form' => $form->createView()]);
        }
    }

然后在树枝上渲染表单:

# templates/product/add.html.twig

{{ form_start(form) }}
    {# your custom code for rendering the form #} 
    {# if you leave default then it should render with bad styles etc. #}
{{ form_end(form) }}

注意:您可能必须更改上述代码以适应您的编码需求,但(理论上)应该可以正常工作-该工作在使用树枝文件生成的页面上有效-您不应具有表单如果您不打算使用表单,请在控制器中输入。只需手动传递数据...