我尝试使用一对多的相对形式渲染形式,但有些字段无法渲染。
AnswerType
形式
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('text', TextType::class, array('attr' => array('class' => 'form-control'), 'label' => false))
->add('isCorrect', CheckboxType::class, array('attr' => array('class' => 'form-control'), 'label' => false))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Answer::class
]);
}
QuestionAnswerType
形式。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', EntityType::class, array(
'class' => Question::class,
'choice_label' => 'title',
));
$builder
->add('answers', CollectionType::class, array(
'entry_type' => AnswerType::class,
'entry_options' => array('label' => false),
'allow_add' => true,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Question::class
]);
}
控制器的片段。
$question = new Question();
$form1 = $this->createForm(QuestionAnswerType::class, $question);
$form1->handleRequest($request);
return $this->render('admin/admin.html.twig',[
'formAnswer' => $form1->createView(),
]);
实体Question
class Question
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="text")
*/
private $title;
/**
* @ORMOneToMany(targetEntity="AppEntityAnswer", mappedBy="question", orphanRemoval=true)
*/
private $answers;
public function __construct()
{
$this->answers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection|Answer[]
*/
public function getAnswers(): Collection
{
return $this->answers;
}
public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setQuestion($this);
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
if ($answer->getQuestion() === $this) {
$answer->setQuestion(null);
}
}
return $this;
}
}
实体Answer
class Answer
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="text")
*/
private $text;
/**
* @ORMColumn(type="boolean")
*/
private $is_correct;
/**
* @ORMManyToOne(targetEntity="AppEntityQuestion", inversedBy="answers")
* @ORMJoinColumn(nullable=false)
*/
private $question;
public function getId(): ?int
{
return $this->id;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getIsCorrect(): ?bool
{
return $this->is_correct;
}
public function setIsCorrect(bool $is_correct): self
{
$this->is_correct = $is_correct;
return $this;
}
public function getQuestion(): ?question
{
return $this->question;
}
public function setQuestion(?Question $question): self
{
$this->question = $question;
return $this;
}
}
在树枝上
<div class="tab-pane fade" id="answer" role="tabpanel" aria-labelledby="answer-tab">
{{ form_start(formAnswer) }}
{{ form_row(formAnswer.title) }}
<h3>Answers</h3>
<ul class="answers">
{% for answers in formAnswer.answers %}
<li>{{ form_row(answers.text) }}</li>
{% endfor %}
</ul>
{{ form_end(formAnswer) }}
</div>
它看起来像 外观如何
表Answer
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| question_id | int(11) | NO | MUL | NULL | |
| text | longtext | NO | | NULL | |
| is_correct | tinyint(1) | NO | | NULL | |
+-------------+------------+------+-----+---------+----------------+
附言最近我问了很多关于symfony的问题,对不起。我只是非常非常想理解它。
渲染问题似乎是您的Question
缺少Answer
实例。反过来,您的窗体不会呈现任何子窗体,因为它没有任何要呈现的内容。您可以通过添加如下所示的答案实例来解决此问题:
// First, the question
$question = new Question();
// Then, some answers
$question->addAnswer(new Answer());
$question->addAnswer(new Answer());
$question->addAnswer(new Answer());
// Lastly, the form
$form1 = $this->createForm(QuestionAnswerType::class, $question);
这应该可以解决问题...