在Symfony3中我试图将表单数据保存到数据库,但它不会转到当前用户的行,而是创建一个新条目

时间:2017-03-25 18:24:23

标签: php symfony symfony-forms

创建的新数据库行中的所有字段都是null,除了isActive和workout_choice,它们设置为1(因为它们应该是,但应该是当前用户 - 具有id,用户信息等的行)。 ,不适合新用户)。出于某种原因,系统没有接收登录的当前用户,但是当var_dumping $ user时,我获得当前用户的信息。

如果有人对我所缺少的东西有任何想法,我将不胜感激。这是我的控制器:

/**
 * @Route ("user/LandingPage", name="user_LandingPage")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function landingPage(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $form = $this->createForm('AppBundle\Form\LandingPageType');
    $form->handleRequest($request);
    $x = $form->getData();
    $user = $this->getCurrentUser();

    if ($form->isValid()) {
        $em->persist($user);
        $em->persist($x);
        $em->flush();
        return $this->chartAction();

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

}

这是我的DefaultController中的getCurrentUser函数:

    public function getCurrentUser()
    {
        $userRepo = $this->getDoctrine()
        ->getRepository('AppBundle:User');
        return $userRepo->findOneBy(array('id' => $this->
        getUser()>getId()));

这是我的FormType文件:

namespace AppBundle\Form;

use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class LandingPageType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array 
    $options)
        {
        $builder
           ->add('workoutChoice', ChoiceType::class, array(
                'label' => 'Choose Your Workout',
                'choices' => array(
                'Pyramid Workout Day 1' => 0,
                'Pyramid Workout Day 2' => 1),
                'attr' => array('style' => 'width:200px;',
                )))
            ->add('submit', SubmitType::class);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(

            'data_class' => 'AppBundle\Entity\User',
            'csrf_protection' => false,
        ));
    }

}

这是我的树枝:

{% extends 'base.html.twig' %}
{% block body %}
<head>

{% block stylesheets %}<link href="{{ asset('css/custom.css') }}" 
rel="stylesheet" media="screen">{% endblock %}
</head>
<body background="{{ asset('sport-1244925_1920.jpg') }}">

<h1>Welcome!</h1>

<h3>Please choose which workout you wish to do today:</h3>

{{ form_start(form, { 'style': 'horizontal', 'col_size': 'xs' }) }}
    {{ form_row(form.workoutChoice) }}
    {{ form_row(form.submit) }}

</body>
{% endblock %}

在我的实体档案中:

/**
 * @ORM\Column(type="smallint", length=6, name="workout_choice", 
       nullable=true)
 */
private $workoutChoice;

/**
 *
 * @return int
 */
public function getWorkoutChoice()
{
    return $this->workoutChoice;
}

/**
 * @param integer $workoutChoice
 */
public function setWorkoutChoice($workoutChoice)
{
    $this->workoutChoice = $workoutChoice;
}

1 个答案:

答案 0 :(得分:0)

尝试将当前用户传递给您的表单。如:

$user = $this->getCurrentUser();
$form = $this->createForm('AppBundle\Form\LandingPageType', $user);

此外,您可以通过简单的方式获取Symfony3控制器中的当前用户:

$user = $this->getUser();