작성된 새 데이터베이스 행의 모든 필드는 isActive 및 workout_choice를 제외한 null입니다. 1로 설정해야합니다 (그렇지만 현재 사용자의 경우 - 새 사용자가 아닌 ID, 사용자 정보 등이있는 행이 있어야 함). 어떤 이유로 시스템은 로그인 한 현재 사용자를 선택하지 않지만 var_dumping $ user를 사용하면 현재 사용자 정보를 얻습니다.Symfony3에서 데이터베이스에 양식 데이터를 유지하려고하지만 현재 사용자 행으로 이동하지 않고 대신 새 항목을 만듭니다.
누군가 내가 누락 된 것에 대한 아이디어가 있다면, 많은 도움이 될 것입니다.
/**
* @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 파일입니다 : 여기 내 컨트롤러의
{% 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 %}
: 여기
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,
));
}
}
그리고 나의 나뭇 가지의
내 엔티티 파일에서 :
/**
* @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;
}
은 당신이 당신의 getCurrentUser() 함수를 좀 보여 주시겠습니까? – jaro1989
공용 함수 getCurrentUser() { $ userRepo = $ this-> getDoctrine() -> getRepository ('AppBundle : User'); return $ userRepo-> findOneBy (array ('id'=> $ this-> getUser() -> getId())); –
사실 새로운 사용자 엔티티를'$ em-> persist ($ x)로 유지하기 때문에 데이터베이스에 새로운 행이 생기고 있습니다'$ user-> setWorkoutChoice ($ x-> getWorkoutChoice())'를 제거하고'$ em-> persist ($ x);를 제거하십시오. – jaro1989