Symfony2를 처음 사용합니다. 나는 fosuserbundle을 사용하여 작은 프로젝트를 만들었다. 등록, 로그인, 로그인 후 또는 등록 및 로그 아웃 후 선택하고 제출하는 라디오 버튼으로 구성된 2 개의 폼이있다. 컨트롤러 액션 코드가 사용자가 로그인 한 후 (사용자가 이전에 로그인하여 선택한 경우) 양식에 사용자가 이미 선택한 라디오 버튼을 표시하거나 기본 선택된 라디오 버튼이있는 기본 양식을 표시하려는 경우 (새 방금 로그인 한 사용자이며 이전에 라디오 버튼을 선택하지 않은 사용자). 내 프로젝트의 코드는 링크 코드와 매우 유사하다 : - How to display the user's already selected radio buttons after logging in symfony2symfony2에 로그인 한 후 사용자가 이미 선택한 라디오 버튼을 표시합니다.
코드는 다음과 같습니다 -
이있는 "구독"엔티티 : -
<?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. 이벤트. 하지만이 문제와 밀접한 관련이 없으므로 공간을 절약하기 위해 여기서 언급하지 않았습니다. 어쨌든, 다양한 개체 사이의 관계는 이것은 "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';
}
}
, 나는 이벤트 (예 : 이벤트 ID)가 그를 가져 오기 위해 노력하고 있어요 사용자가 데이터베이스에서 선택한 후 "formtype"에 전달하여 이러한 이벤트 (즉, 이벤트 id)를 사용하고 로그인 한 후 원래의 라디오 버튼을 선택한 양식을 표시합니다. 그러나 오류가 발생합니다. any1은 로그인 한 사용자에게 라디오 버튼의 선택된 값을 표시하는 방법에 관해서 도움이 될 수 있습니까 ???
<?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();//This contains users selected radio buttons(if any) and this is being passed to the formtype "SubscriptionsType"
$form = $this->createForm(new SubscriptionsType($subscriptionsreg), $subscriptions);
$form->handleRequest($request);
if ($form->isValid()) {
//Save to the Database
$em = $this->getDoctrine()->getManager();
$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($subscriptionsreg), 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');
}
}
서브 스크립 션과 사용자간에 관계가 있습니까? Beucase 당신이 그것을 필요로하고 난 귀하의 코드에서 사용자 엔티티와 어떤 정의를 볼 수 없었다? –