我正在嘗試為測驗創建 Symfony 表單,每個問題都有問題和選擇(請參閱物體代碼)。我有RequestQuestion包含description, isActive, choices. 選擇是另一個包含name, correct和relation質疑的物體。最后Request包含 ManyToMany 選項(表示用戶勾選了此選項)。
但是現在我有一個問題,我需要以某種方式在表單中按問題分組選擇(使用具有多個和擴展 true 的 EntityType)。不 - EntityType 的 group_by 不適用于多個 = 擴展 = true。這僅適用于選擇框。
后來我添加到Request關系到Question. 這解決了一半的問題——我現在可以在 FormType 中將 CollectionType 添加到問題中(這是另一個 FormType RequestQuestionType)。請注意,這會在 DB 中產生冗余,這是不好的。(實際上并沒有。我不知道該請求使用了哪些問題,因為問題可以通過設定isActive或添加新問題及時更改)。但現在的問題在于RequestQuestionType我無法添加答案,因為問題沒有這種關系(只有Requestor QuestionChoice)。
問題是我怎樣才能得到這個表格的答案?我不能使用 parrent ( RequestFormType),因為我不能按問題對選擇進行分組,并且在問題 ( RequestQuestionType) 中我不能添加關系。貝婁我正在發送代碼的當前狀態。
要求
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="uuid")
*/
private $uuid;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="requests")
* @ORM\JoinColumn(nullable=false)
*/
private $User;
/**
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $resolved;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private $resolvedBy;
/**
* @ORM\Column(type="string", length=32)
*/
private $state;
/**
* @ORM\Column(type="string", length=255)
*/
private $address;
/**
* @ORM\ManyToMany(targetEntity=RequestQuestion::class, inversedBy="requests")
*/
private $questions;
/**
* @ORM\ManyToMany(targetEntity=RequestQuestionChoice::class, inversedBy="scholarRequestsAnswers")
*/
private $answers;
請求問題
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="boolean")
*/
private $isActive;
/**
* @ORM\OneToMany(targetEntity=RequestQuestionChoice::class, mappedBy="Question", orphanRemoval=true)
*/
private $choices;
/**
* @ORM\ManyToMany(targetEntity=Request::class, mappedBy="questions")
*/
private $requests;
請求問題選擇
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="boolean")
*/
private $correct;
/**
* @ORM\ManyToOne(targetEntity=RequestQuestion::class, inversedBy="choices")
* @ORM\JoinColumn(nullable=false)
*/
private $Question;
請求表單型別
class RequestFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('address', TextType::class, [
'constraints' => [
new NotBlank([
'message' => "Zadejte adresu"
])
]
])
->add('questions', CollectionType::class, [
'entry_type' => RequestQuestionType::class,
'entry_options' => [
'questions' => $builder->getData()->getQuestions()
]
])
->add('tos', CheckboxType::class, [
'mapped' => false,
'value' => false,
'constraints' => [
new IsTrue([
'message' => "Musíte souhlasit s na?imi podmínkami pou?ití"
])
]
])
->add('Submit', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Request::class
]);
}
}
請求問題型別
class RequestQuestionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$index = str_replace(["[", "]"], "", $builder->getPropertyPath());
$builder
->add('???', EntityType::class, [
'class' => RequestQuestionChoice::class,
'choice_label' => 'name',
'choices' => $options["questions"][$index]->getChoices(),
'expanded' => true,
'multiple' => true
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'questions' => []
]);
}
}
注意:由于某種原因,來自的問題RequestFormType不會作為資料傳遞(data = null),所以這就是我將它們作為 entry_options 傳遞的原因。但是在RequestQuestionType它呼叫它的次數與問題數一樣多,所以這有點奇怪,但我設法通過 entry_otpions 解決它并使用 propertyPath 中的索引。
注意:請求是使用虛擬資料預先構建的 - 問題并傳遞給此表單。
注意:我之前也嘗試過分解 Request - RequestChoice as RequestAnwer 中的 manyToMany 關系,并在用戶勾選或不勾選選項時使用 bool 并預先生成對 questionChoices 的所有答案。但是按問題對選擇進行分組的問題也存在,所以我也無法讓它發揮作用。
uj5u.com熱心網友回復:
解決了。
我已經添加RequestQuestionAnswers了 OneToMany 到RequestQuestion(RequestQuestionAnswers有一個問題)和 ManyToMany 到的答案RequestQuestionChoice。通過這種方式,這個新物體與問題 1:1 系結,對于每個問題,我可以為每個問題單獨生成 EntityType 并生成問題的選擇。
如果有人會遇到類似的問題,我將在此處粘貼最終代碼。這個問題也很有幫助:Create quizz form symfony
注意:遺憾的是,我不能將它用于多個 false ,因為RequestQuestion.requestAnswersis Collection 所以它會引發錯誤:Entity of type "Doctrine\Common\Collections\ArrayCollection" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?
請求表單型別
class RequestFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('address', TextType::class, [
'constraints' => [
new NotBlank([
'message' => "Zadejte adresu"
])
],
])
->add('requestAnswers', CollectionType::class, [
'entry_type' => RequestQuestionType::class,
'entry_options' => [
'request' => $builder->getData(),
'label_attr' => [
'class' => 'd-none'
]
],
'label' => 'Dotazník'
])
->add('tos', CheckboxType::class, [
'mapped' => false,
'value' => false,
'constraints' => [
new IsTrue([
'message' => "Musíte souhlasit s na?imi podmínkami pou?ití"
])
],
'label' => 'Souhlasím s podmínkami pou?ití'
])
->add('Submit', SubmitType::class, [
'label' => 'Odeslat'
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Request::class
]);
}
}
請求問題型別
class RequestQuestionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$index = str_replace(["[", "]"], "", $builder->getPropertyPath());
/** @var RequestAnswers $answers */
$answers = $options["request"]->getRequestAnswers()[$index];
$builder
->add('selectedChoices', EntityType::class, [
'class' => RequestQuestionChoice::class,
'choices' => $answers->getQuestion()->getChoices(),
'choice_label' => 'name',
'label' => $answers->getQuestion()->getDescription(),
'expanded' => true,
'multiple' => true
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => RequestAnswers::class,
'request' => null
]);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/448594.html
