簡體   English   中英

在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和execution_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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM