登录symfony2后如何显示用户已选择的单选按钮 [英] How to display the user's already selected radio buttons after logging in symfony2

查看:101
本文介绍了登录symfony2后如何显示用户已选择的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Symfony2的新手,并且使用fosuserbundle。
我已经使用fosuserbundle创建了一个小项目,它具有注册,登录,2个单选按钮的形式,可在登录或注册后进行选择和提交。
用户登录并选择各种单选按钮并单击提交后,将被转到成功页面。但是当用户登录后再次下次,则用户之前选择的单选按钮将显示为NOT,但会显示已选中默认单选按钮的表单。我想在用户登录后显示用户已经选择的单选按钮(如果他早先通过登录选择)或默认窗体(对于刚刚登录的新用户,而不是早期选择任何单选按钮)。

I am new to Symfony2 and using fosuserbundle. I have created a small project using fosuserbundle which has a registration, login, 2 forms consisting of radio buttons to choose and submit after logging in or registering and a logout. After a user logins and selects the various radio buttons and clicks on "Submit", he is taken to the "Success Page". But the next time again when the user logins, then the user's previously selected radio buttons are "NOT" shown, but the form with the default radio buttons checked are shown. I wanted to display the user's already selected radio buttons after the user logs in(if he has selected it earlier by logging in) or the default form(for a new user who has just now logged in and not selected any radio buttons earlier).

这是订阅实体: -

This is the "Subscriptions" entity:-

<?php

namespace InstituteEvents\StudentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="subscriptions")
*/
class Subscriptions {

/**
 * @ORM\Id
 * @ORM\Column(name="id",type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var integer
 * @ORM\Column(name="Event1", type="integer")
 * @ORM\ManyToOne(targetEntity="Events")
 * @ORM\JoinColumn(name="Event1", referencedColumnName="id")
 **/    
protected $event1;

/**
 * @var integer
 * @ORM\Column(name="Event2", type="integer")
 * @ORM\ManyToOne(targetEntity="Events")
 * @ORM\JoinColumn(name="Event2", referencedColumnName="id")
 **/

protected $event2;

/**
 * @var integer
 * @ORM\Column(name="Event3", type="integer")
 * @ORM\ManyToOne(targetEntity="Events")
 * @ORM\JoinColumn(name="Event3", referencedColumnName="id")
 **/
protected $event3;

/**
 * @ORM\OneToOne(targetEntity="Students")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 **/
 protected $students;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set id
 *
 * @param integer $id
 * @return Subscriptions
 */
public function setId($id) 
{
    $this->id = $id;

    return $this;
}

/**
 * Set event1
 *
 * @param integer $event1
 * @return Subscriptions
 */
public function setEvent1($event1)
{
    $this->event1 = $event1;

    return $this;
}

/**
 * Get event1
 *
 * @return integer 
 */
public function getEvent1()
{
    return $this->event1;
}

/**
 * Set event2
 *
 * @param integer $event2
 * @return Subscriptions
 */
public function setEvent2($event2)
{
    $this->event2 = $event2;

    return $this;
}

/**
 * Get event2
 *
 * @return integer 
 */
public function getEvent2()
{
    return $this->event2;
}

/**
 * Set event3
 *
 * @param integer $event3
 * @return Subscriptions
 */
public function setEvent3($event3)
{
    $this->event3 = $event3;

    return $this;
}

/**
 * Get event3
 *
 * @return integer 
 */
public function getEvent3()
{
    return $this->event3;
}
}

注意: - 还有2个实体,即:
1.学生
2.活动。
但它并不是与这个问题密切相关,所以为了节省空间,我没有在这里提到。无论如何,各个实体之间的关系已经被正确设置了

Note:- There are 2 more entities namely :- 1. Students 2. Events. But it is not so closely related to this problem, so to save space I have not mentioned it here. Anyways, the relationships between the various entities have been set properly

这是订阅类型: -

This is the "SubscriptionsType" :-

<?php

namespace InstituteEvents\StudentBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;


class SubscriptionsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

->add('event1', 'choice', array('choices' => array('1' => 'Tourism', '2' => 'Food party',     '3'     => 'South korean food', '4' => 'Cooking', '5' => 'None of the above'), 'data' => '5', 'expanded' => true, 'multiple' => false))

->add('event2', 'choice', array('choices' => array('6' => 'Cricket', '7' => 'Football', '8' => 'Hockey', '9' => 'Baseball', '10' => 'Polo', '5' => 'None of the above'), 'data' => '5', 'expanded' => true, 'multiple' => false))

->add('event3', 'choice', array('choices' => array('11' => 'Game 1', '12' => 'Game 2', '13' => 'Game 3', '14' => 'Game 4', '15' => 'Game 5', '5' => 'None of the above'), 'data' => '5', 'expanded' => true, 'multiple' => false))

->add('register', 'submit');
}

public function getName()
{
    return 'subscriptions';
}
}

这是Controller(需要写什么代码在这个Controller中解决上述问题???): -


This is the Controller (What code needs to be written in this Controller to solve the above problem ???) :-

namespace InstituteEvents\StudentBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use InstituteEvents\StudentBundle\Entity\Subscriptions;
use InstituteEvents\StudentBundle\Form\Type\SubscriptionsType;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller {

 /**
 * This action displays the form for the events of day 1.
 * 
 * @param Request $request
 * @return A Confirmation page on success
 */
public function eventsoneAction(Request $request) {

    //Get current time and date

     date_default_timezone_set('Europe/Paris');
     $current_date = date('Y/m/d h:i:s a', time());

    //Set expiration date

    $deadline1 = $this->container->getParameter('deadline_day1');
    $date=date_create($deadline1,timezone_open("Europe/Paris"));

    $em = $this->getDoctrine()->getManager();
    $subscriptions = new Subscriptions();

    $form = $this->createForm(new SubscriptionsType, new Subscriptions());
    $form->handleRequest($request);

    if ($form->isValid()) {
    //Save to the Database    
    $subscriptions = $form->getData();

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

      return $this->redirect($this->generateUrl('institute_events_student_eventsregistered'));
    }

     if($current_date > date_format($date,"Y/m/d h:i:s a")) {
       return $this->render('InstituteEventsStudentBundle:Default:registrationsclosed.html.twig');
       }
     else {
          $form = $this->createForm(new SubscriptionsType, new Subscriptions());

          return $this->render('InstituteEventsStudentBundle:Default:eventsday1.html.twig', array('form' => $form ->createView()));
     }
}

/**
 * This action displays the Confirmation page on success.
 * 
 * @param Request $request
 * @return A Confirmation page on success
 */
public function eventsregisteredAction() {
    return $this->render('InstituteEventsStudentBundle:Default:eventsregistered.html.twig');
}

}

在下面的控制器中,我试图获取用户从数据库中选择的事件(即事件id),并将其传递给formtype,以便它们使用这些事件(即事件id),并显示用户的原始无线电按钮在登录后选择表单,但会引发错误。可以any1帮助如何显示登录的用户他/她选择的单选按钮的值?

In the below controller, I am trying to fetch the events(i.e event id) that the user has selected from the database and passing it to the "formtype" so that it uses these events(i.e event id) and displays the users original radio button selected form after logging in. But it throws an error. Can any1 help as to how to show the logged in user his/her selected values of the radio buttons ???

 <?php

namespace InstituteEvents\StudentBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use InstituteEvents\StudentBundle\Entity\Subscriptions;
use InstituteEvents\StudentBundle\Form\Type\SubscriptionsType;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller {

 /**
 * This action displays the form for the events of day 1.
 * 
 * @param Request $request
 * @return A Confirmation page on success
 */
public function eventsoneAction(Request $request) {

    //Get current time and date

     date_default_timezone_set('Europe/Paris');
     $current_date = date('Y/m/d h:i:s a', time());

    //Set expiration date

    $deadline1 = $this->container->getParameter('deadline_day1');
    $date=date_create($deadline1,timezone_open("Europe/Paris"));

    $em = $this->getDoctrine()->getManager();
    $subscriptions = new Subscriptions();

    //Check if events already selected
    $repository = $this->getDoctrine()
            ->getRepository('InstituteEventsStudentBundle:Subscriptions');

    $query = $repository->createQueryBuilder('p')
             ->select('p.event1','p.event2','p.event3','p.event4')
             ->where('p.id = p.getId()')
             ->getQuery();

    $subscriptionsreg = $query->getResult();

    $form = $this->createForm(new SubscriptionsType($subscriptionsreg), new Subscriptions());
    $form->handleRequest($request);

    if ($form->isValid()) {
    //Save to the Database    
    $subscriptions = $form->getData();

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

      return $this->redirect($this->generateUrl('institute_events_student_eventsregistered'));
    }

     if($current_date > date_format($date,"Y/m/d h:i:s a")) {
       return $this->render('InstituteEventsStudentBundle:Default:registrationsclosed.html.twig');
       }
     else {
          $form = $this->createForm(new SubscriptionsType, new Subscriptions());

          return $this->render('InstituteEventsStudentBundle:Default:eventsday1.html.twig', array('form' => $form ->createView()));
     }
}

/**
 * This action displays the Confirmation page on success.
 * 
 * @param Request $request
 * @return A Confirmation page on success
 */
public function eventsregisteredAction() {
    return $this->render('InstituteEventsStudentBundle:Default:eventsregistered.html.twig');
}

}

推荐答案

这是因为您每次在控制器中创建一个新的订阅实体:

It's because you are creating a new Subscriptions entity every time in the controller:

$subscriptions = new Subscriptions();

而您甚至没有在 createForm

$form = $this->createForm(new SubscriptionsType, new Subscriptions());

加上订阅与您的用户相关联,我假设我是一个学生实体。要解决这个问题,您必须执行以下操作:

Plus there's no point where the Subscription is associated with your user, who I'm assuming is a Student entity. To fix this, you must do something like:

if (!($subscriptions = $this->getUser()->getSubscriptions())) { // This line may not be right
    $subscriptions = new Subscriptions();
    $subscriptions->setStudents($this->getUser());
}

$form = $this->createForm(new SubscriptionsType, $subscriptions);

我怀疑你的开发中还有其他漏洞,所以这不会是完整的解决您的问题,但它解决了您的问题的要点。

I suspect that there are other "holes" in your development, so this won't be a complete solution to your issues, but it addresses the main point to your question.

可能不正确的行也可能是这样的:

The line that may not be right could also be something like:

if (!($subscriptions = $em->getRepository('InstituteEventsStudentBundle:Subscriptions')->findBy(...))) {

这篇关于登录symfony2后如何显示用户已选择的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆