我想通过表单在表格之间建立多对多关系。我正在为一个游戏开发一个有几个问题的客户。他可以注册并回答给定的问题。我要设置的关系是“客户”和“答案表”之间。
因此,我呈现了客户所需的字段。现在,我想用可能的答案来提出一些问题。
在给定的代码中,您可以看到我在EventListener中添加了“答案”表单字段,作为“ Answer :: class”的EntityType的CollectionType,但是我在“ data”处设置的数组的类型为“ Question :: class” “。
现在,他正在呈现我的三个问题,但每个答案都有可能-与实际呈现的问题无关。
我尝试使用“ query_builder”选项,但未找到解决方案。
ERM:https://i.ibb.co/bgjmPsF/adventskalender.png
Customer.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
*/
class Customer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, unique=true, nullable=false)
*/
private $email;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $dse_checked;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $reminder_checked;
/**
* @ORM\Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
*/
private $registration;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $newsletter;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Answer", inversedBy="customers")
* @ORM\JoinTable(name="customer_answer")
*/
private $answers;
public function __construct()
{
$this->registration = new \DateTime("now");
$this->answers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getDseChecked(): ?bool
{
return $this->dse_checked;
}
public function setDseChecked(?bool $dse_checked): self
{
$this->dse_checked = $dse_checked;
return $this;
}
public function getReminderChecked(): ?bool
{
return $this->reminder_checked;
}
public function setReminderChecked(?bool $reminder_checked): self
{
$this->reminder_checked = $reminder_checked;
return $this;
}
public function getRegistration(): ?\DateTimeInterface
{
return $this->registration;
}
public function setRegistration(\DateTimeInterface $registration): self
{
$this->registration = $registration;
return $this;
}
public function getNewsletter(): ?bool
{
return $this->newsletter;
}
public function setNewsletter(?bool $newsletter): self
{
$this->newsletter = $newsletter;
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;
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
}
return $this;
}
function __toString(): string
{
return $this->getName();
}
}
Question.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
*/
class Question
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $value;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Winning", inversedBy="questions")
*/
private $winning;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Answer", mappedBy="question", cascade={"persist"})
*/
private $answers;
public function __construct()
{
$this->answers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(string $value): self
{
$this->value = $value;
return $this;
}
public function getWinning(): ?Winning
{
return $this->winning;
}
public function setWinning(?Winning $winning): self
{
$this->winning = $winning;
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);
// set the owning side to null (unless already changed)
if ($answer->getQuestion() === $this) {
$answer->setQuestion(null);
}
}
return $this;
}
/**
* @return string
*/
public function __toString(): string
{
return $this->getValue();
}
}
Answer.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\AnswerRepository")
*/
class Answer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $value;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="answers")
*/
private $question;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isRight;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Customer", mappedBy="answers")
* @ORM\JoinTable(name="customer_answer")
*/
private $customers;
public function __construct()
{
$this->customers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(string $value): self
{
$this->value = $value;
return $this;
}
public function getQuestion(): ?Question
{
return $this->question;
}
public function setQuestion(?Question $question): self
{
$this->question = $question;
return $this;
}
public function getIsRight(): ?bool
{
return $this->isRight;
}
public function setIsRight(?bool $isRight): self
{
$this->isRight = $isRight;
return $this;
}
/**
* @return Collection|Customer[]
*/
public function getCustomers(): Collection
{
return $this->customers;
}
public function addCustomer(Customer $customer): self
{
if (!$this->customers->contains($customer)) {
$this->customers[] = $customer;
$customer->addAnswer($this);
}
return $this;
}
public function removeCustomer(Customer $customer): self
{
if ($this->customers->contains($customer)) {
$this->customers->removeElement($customer);
$customer->removeAnswer($this);
}
return $this;
}
/**
* @return string|null
*/
public function __toString()
{
return $this->getValue();
}
}
CustomerType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->questions= $options['questions'];
$builder
->add('name', TextType::class, ['label' => 'Name', 'mapped' => true,])
->add('email', TextType::class, ['label' => 'E-Mail'])
->add('dse_checked', CheckboxType::class, ['label' => 'Datenschutzbestimmungen akzeptiert'])
->add('reminder_checked', CheckboxType::class, ['label' => 'Tägliche Erinnerungen'])
->add('newsletter', CheckboxType::class, ['label' => 'Newsletter abonieren'])
->addEventListener(FormEvents::PRE_SET_DATA, [
$this,
'onPreSetData'
])
->add('submit', SubmitType::class, ['label' => 'Speichern']);
}
/**
* @param FormEvent $event
*/
public function onPreSetData(FormEvent $event)
{
$form = $event->getForm();
$questions = $this->questions;
foreach ($questions as $wKey => $question) {
$answers = array_merge($answers, $question->getAnswers()->getValues());
}
$form->add('answers', CollectionType::class, [
'entry_type' => EntityType::class,
'data' => $questions,
'by_reference' => false,
'mapped' => true,
'entry_options' => [
'label' => 'Label',
'class' => Answer::class,
]
]);
}
最后,我想在客户公式中列出一个带有相关答案选项的问题列表,以便将给定的答案保存到我的客户实体中。