gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:04:13 27 4
gpt4 key购买 nike

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

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

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

问题已经存储在 DB Question.table 中。

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

你能帮我让它工作吗?我只能为最后一个问题显示一个答案,我在 Controller 逻辑或我的 FormType 中做错了什么?

Answer.entity

    <?php

namespace SurveyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

/**
* @var string
*
* @ORM\Column(name="text", type="string", length=255)
*/
private $text;

/**
* Id from Company Entity
*
* @var int
*
* @ORM\ManyToOne(targetEntity="SurveyBundle\Entity\Survey", inversedBy="answer", cascade={"persist"})
*
*/
private $survey;

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


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

/**
* Set text
*
* @param string $text
*
* @return Answer
*/
public function setText($text)
{
$this->text = $text;

return $this;
}

/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}

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

/**
* @param int $survey
*/
public function setSurvey($survey)
{
$this->survey = $survey;
}

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

/**
* @param mixed $question
*/
public function setQuestion($question)
{
$this->question = $question;
}

}

问题实体

<?php

namespace SurveyBundle\Entity;

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

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

/**
* @var string
*
* @ORM\Column(name="text", type="string", length=255)
*/
private $text;


public function __toString()
{
return (string) $this->text;
}

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

/**
* Set text
*
* @param string $text
*
* @return Question
*/
public function setText($text)
{
$this->text = $text;

return $this;
}

/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}



}

调查实体

<?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;
}




}

Survey.controller

<?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()));
}
}

Survey.FormType

<?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';
}


}

表单 View : front.survey

我只有一个答案字段,仅用于问题 2。每个问题我都需要一个。

另外,当我尝试提交时出现此错误:

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in .../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605 and defined

最佳答案

您正在使用 multiple=>true 语句呈现所有问题,但这并没有将答案与每个问题相关联。 THEN 你的表单呈现一个答案,所以你只会看到一个答案。

我认为您需要的是一组表单,请参阅文档:http://symfony.com/doc/current/form/form_collections.html

首先为每个问题/答案组合构建一个表单,然后呈现来自 Survey.FormType 的表单集合。

关于php - 使用现有和不存在的实体创建表单/调查 (Symfony 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com