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

In Symfony3 I am trying to persist form data to the database, but it doesn't go to the current user's row, instead creates a new entry

创建的新数据库行中的所有字段都为 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;
}

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

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

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

$user = $this->getUser();