Php 使用现有和不存在的实体创建表格/调查(Symfony 3)

Php 使用现有和不存在的实体创建表格/调查(Symfony 3),php,mysql,forms,symfony,survey,Php,Mysql,Forms,Symfony,Survey,我正在尝试在Symfony 3.2.4和PHP 5.6.28中为我的应用程序创建一个动态调查/表单包 我有3个实体:调查/问题/答案 第一步是为每个用户创建一个调查。 问题必须与相关答案字段一起显示在前面。 提交时,它们将存储在DB classic中 问题已存储在DB Question.table中 我想让表格生效。显示数据库中存储的所有问题并提交答案(与问题相关)。此外,所有答案必须链接到调查,以便我可以显示特定的调查结果(在用户完成后) 你能帮我把它做好吗? 对于最后一个问题,我只能显示一个

我正在尝试在Symfony 3.2.4和PHP 5.6.28中为我的应用程序创建一个动态调查/表单包

我有3个实体:调查/问题/答案

第一步是为每个用户创建一个调查。 问题必须与相关答案字段一起显示在前面。 提交时,它们将存储在DB classic中

问题已存储在DB Question.table中

我想让表格生效。显示数据库中存储的所有问题并提交答案(与问题相关)。此外,所有答案必须链接到调查,以便我可以显示特定的调查结果(在用户完成后)

你能帮我把它做好吗? 对于最后一个问题,我只能显示一个答案,我在控制器逻辑或表单类型中出错了?

答案:实体


您正在使用multiple=>true语句呈现所有问题,但这不会将答案与每个问题关联起来。然后表单呈现一个答案,因此您只能看到一个答案

我认为您需要的是一系列表格,请参见文档:


首先为每个问题/答案组合构建一个表单,然后呈现调查中的表单集合。FormType。

好的,多亏了@ehymel,我找到了一个解决方案

在我的调查表单生成器中,我现在有2个CollectionType:问答 每个人都有自己的模板

我在控制员中做了一个练习,将问答与我的调查对象联系起来,另一个练习是在提交后坚持我的答案

我还修改了我的实体关系

控制器:

$questions = $em->getRepository('SurveyBundle:Question')->findAll();

    $survey = new Survey();

    foreach ($questions as $question) {
        $answer = new Answer();
        $survey->getQuestion()->add($question);
        $answer->setQuestion($question);
        $survey->getAnswer()->add($answer);
    }

    $form = $this->createForm(SurveyType::class, $survey);

    $form->handleRequest($request);

    if ($request->getMethod() === 'POST') {
        if ($form->isSubmitted() && $form->isValid()) {
            $survey->setExperience($experience);
            $em->persist($survey);
            $em->flush();

            foreach ($survey->getAnswer() as $answer) {
                $answer->setSurvey($survey);
                $em->persist($answer);
                $em->flush();
            }
        } else {
            //ERROR
            trigger_error('Error - Experience hasn\'t been completed - Contact a Wizard');
        }
    }
调查类型:

class SurveyType extends AbstractType{

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */

private $name = 'default_name';

public function getName(){
    return $this->name;
}

public function setName($name){
    $this->name = $name;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'question',
            CollectionType::class,
            array(
                'entry_type' => QuestionType::class)
        )
        ->add(
            'answer',
            CollectionType::class,
            array(
            'entry_type' => AnswerType::class)
        );
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'SurveyBundle\Entity\Survey'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'referencebundle_survey';
}

}

你的问题是什么?
<?php

namespace SurveyBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Survey
 *
 * @ORM\Table(name="survey")
 * @ORM\Entity(repositoryClass="SurveyBundle\Repository\SurveyRepository")
 */
class Survey
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * Id from Question Entity
     *
     * @ORM\OneToMany(targetEntity="SurveyBundle\Entity\Question", mappedBy="survey")
     */
    private $question;


    function __construct()
    {
        $this->question = new ArrayCollection();
    }

    public function getQuestion()
    {
        return $this->question;
    }

    /**
     * Id from Answer Entity
     *
     * @ORM\OneToMany(targetEntity="SurveyBundle\Entity\Answer", mappedBy="survey")
     */
    private $answer;

    public function setAnswer($answer)
    {
        $this->answer = $answer;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getAnswer()
    {
        return $this->answer;
    }

    /**
     * @param Question $question
     */
    public function addQuestion(Question $question)
    {
        $this->question[] = $question;
    }




}
<?php

namespace SurveyBundle\Controller;

use SurveyBundle\Entity\Survey;
use SurveyBundle\Form\SurveyType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/survey", name="survey")
     */
    public function indexAction(Request $request)
    {

        $em = $this->getDoctrine()->getEntityManager();
        $questions = $em->getRepository('SurveyBundle:Question')->findAll();

        $survey = new Survey();

        //$ListProduits->setProduits($produits);

        foreach ($questions as $question)
            $survey->addQuestion($question);

        $form = $this->createForm(SurveyType::class, $survey);

        $form->handleRequest($request);

        if ($request->getMethod() === 'POST') {
            if ($form->isSubmitted() && $form->isValid()) {

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

                $data = $form->getData();

                var_dump($data);

                echo 'OK2';

            }else {
                //ERROR
                trigger_error('Error - Survey hasn\'t been completed - Contact a Wizard');
            }
        }


        return $this->render('SurveyBundle:Default:index.html.twig',  array('form' => $form->createView()));
    }
}
<?php

namespace SurveyBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SurveyType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            //->add('name','text')
            ->add('question', EntityType::class,
                    'class' => 'SurveyBundle:Question',
                    'mapped' => false,
                    "multiple" => true,
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('q')
                            ->orderBy('q.text', 'ASC');
                    },
            ))
            ->add(
                'answer',
                AnswerType::class
            )
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'SurveyBundle\Entity\Survey'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'referencebundle_survey';
    }


}
$questions = $em->getRepository('SurveyBundle:Question')->findAll();

    $survey = new Survey();

    foreach ($questions as $question) {
        $answer = new Answer();
        $survey->getQuestion()->add($question);
        $answer->setQuestion($question);
        $survey->getAnswer()->add($answer);
    }

    $form = $this->createForm(SurveyType::class, $survey);

    $form->handleRequest($request);

    if ($request->getMethod() === 'POST') {
        if ($form->isSubmitted() && $form->isValid()) {
            $survey->setExperience($experience);
            $em->persist($survey);
            $em->flush();

            foreach ($survey->getAnswer() as $answer) {
                $answer->setSurvey($survey);
                $em->persist($answer);
                $em->flush();
            }
        } else {
            //ERROR
            trigger_error('Error - Experience hasn\'t been completed - Contact a Wizard');
        }
    }
class SurveyType extends AbstractType{

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */

private $name = 'default_name';

public function getName(){
    return $this->name;
}

public function setName($name){
    $this->name = $name;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'question',
            CollectionType::class,
            array(
                'entry_type' => QuestionType::class)
        )
        ->add(
            'answer',
            CollectionType::class,
            array(
            'entry_type' => AnswerType::class)
        );
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'SurveyBundle\Entity\Survey'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'referencebundle_survey';
}