我是Symfony框架的新手。我想做一个简单的测验,问题和答案很少。我的问题是只显示一个问题和答案。当我尝试它时,它会向我显示问题的下拉列表以及所有问题的所有答案。
我的数据库是:
questions:
- id PK
- question
answers:
- id PK
- answer
choices:
- id PK
- questions_id FK
- answers_id FK
- isCorrect
QuizController:
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Questions;
use AppBundle\Entity\Answers;
use AppBundle\Entity\Choices;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Form\QuizType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class QuizController extends Controller
{
/**
* @Route("/quiz", name="quiz_index")
* @param Request $request
* @return Response
*/
public function formAction(Request $request)
{
$choice = new Choices();
$form = $this->createForm(QuizType::class, $choice);
return $this->render("Quiz/quiz.html.twig", ["form" => $form->createView()]);
}
}
QuizType:
<?php
namespace AppBundle\Form;
use AppBundle\Entity\Answers;
use AppBundle\Entity\Questions;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class QuizType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('question')
->add('answer', EntityType::class, array(
'class' => Answers::class,
'multiple' => false,
'expanded' => true,))
->add("submit", SubmitType::class, ["label" => "Zapisz"]);
}
}
quiz.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{{ form(form, {'attr': {'novalidate': 'novalidate'}}) }}
{% endblock %}