Я создаю приложение для генерации тестовых вопросов, в котором я могу добавить ответы N на один вопрос. (Я могу добавить эти ответы через jQuery) .
Как мне правильно отправить это сообщение? Я читал о событиях формы, но понятия не имею, как это реализовать в этой ситуации.
Вот мой текущий код (я использую Symfony 4):
Вопрос Entity
/**
* @ORM\Entity(repositoryClass = "App\Repository\QuestionRepository")
* @ORM\Table(name = "question")
*/
class Question
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type = "integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity = "App\Entity\User", inversedBy = "questions")
* @ORM\JoinColumn(nullable=false, onDelete = "CASCADE")
*/
private $user;
/**
* @ORM\Column(type = "string", length=100, nullable=true)
*/
private $label;
/**
* @ORM\Column(type = "text", length=2000)
*/
private $content;
/**
* @ORM\Column(type = "string", length=45, options = {"default": "single"})
*/
private $type;
/**
* @ORM\OneToMany(targetEntity = "App\Entity\QuestionAnswer", mappedBy = "question")
*/
private $answers;
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @return string
*/
public function getLabel(): string
{
return $this->label;
}
/**
* @param string $label
*/
public function setLabel(string $label): void
{
$this->label = $label;
}
/**
* @return string
*/
public function getContent(): string
{
return $this->content;
}
/**
* @param string $content
*/
public function setContent(string $content): void
{
$this->content = $content;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type): void
{
$this->type = $type;
}
/**
* @return Collection|QuestionAnswer[]
*/
public function getAnswers()
{
return $this->answers;
}
}
Вопрос ответ Entity
class QuestionAnswer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type = "integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity = "App\Entity\Question", inversedBy = "answers")
* @ORM\JoinColumn(nullable=false, onDelete = "CASCADE")
*/
private $question;
/**
* @ORM\Column(type = "text")
*/
private $content;
/**
* @ORM\Column(type = "boolean")
*/
private $is_correct;
public function getId()
{
return $this->id;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getIsCorrect(): ?bool
{
return $this->is_correct;
}
public function setIsCorrect(bool $is_correct): self
{
$this->is_correct = $is_correct;
return $this;
}
/**
* @return self
*/
public function getQuestion(): self
{
return $this->question;
}
/**
* @param Question $question
*/
public function setQuestion(Question $question): void
{
$this->question = $question;
}
}
@HeinzSchilling Я знаю, что передать его как массив, но как правильно назначить его в БД?
Лучшим вариантом было бы следовать инструкциям по встраиванию коллекции форм symfony.com/doc/current/form/form_collections.html






Вам необходимо следовать документации:
Где в вашем случае Question = Task и QuestionAnswer = Tag.
попробуйте изменить свой код на этот и сами увидите разницу:
в сущности Вопрос:
use Doctrine\Common\Collections\ArrayCollection;
//....
/**
* @ORM\Entity(repositoryClass = "App\Repository\QuestionRepository")
* @ORM\Table(name = "question")
*/
class Question
{
//....
/**
* @ORM\ManyToMany(targetEntity = "App\Entity\QuestionAnswer")
* @var answers
*/
private $answers;
public function setAnswers(answers $answers)
{
$this->answers = $answers;
}
public function getAnswers()
{
return $this->answers;
}
public function __construct()
{
$this->answers = new ArrayCollection();
}
}
затем удалите поле вопроса из объекта Вопрос ответ и обновите схему базы данных
Ответ в виде массива.