Даны три сущности Doctrine.
Ответы на вопросы анкеты Answer
:
/**
* @ORM\Entity(repositoryClass="App\Repository\AnswerRepository")
*/
class Answer
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Options", inversedBy="answers")
* @ORM\JoinColumn(nullable=true)
*/
private $options;
public function getOptions(): ?options
{
return $this->options;
}
public function setOptions(?options $options): self
{
$this->options = $options;
return $this;
}
// ...
}
Варианты ответов на вопросы анкеты Options
:
class Options
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\Answer", mappedBy="options")
*/
private $answers;
public function __construct()
{
$this->answers = new ArrayCollection();
}
/**
* @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->setOptions($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->getOptions() === $this) {
$answer->setOptions(null);
}
}
return $this;
}
// ...
}
И сущность с вопросами анкеты Question. Отправляю POST-запрос с ID вариантов ответов на вопросы анкеты:
{
"options": [
"1",
"2"
],
}
Таким образом:
$answer = new Answer();
$form = $this->createFormBuilder($answer)
->add('options', EntityType::class, [
'class' => Options::class,
'multiple' => true
]);
Получают ответ:
{
"code": 500,
"message": "Expected argument of type \"App\\Entity\\Options or null\", \"Doctrine\\Common\\Collections\\ArrayCollection\" given at property path \"options\"."
}
dev.log:
[2019-07-05 15:16:25] request.CRITICAL: Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "App\Entity\Options or null", "Doctrine\Common\Collections\ArrayCollection" given at property path "options"." at C:\xampp\htdocs\suppliers\src\vendor\symfony\property-access\PropertyAccessor.php line 197 {"exception":"[object] (Symfony\\Component\\PropertyAccess\\Exception\\InvalidArgumentException(code: 0): Expected argument of type \"App\\Entity\\Options or null\", \"Doctrine\\Common\\Collections\\ArrayCollection\" given at property path \"options\". at C:\\xampp\\htdocs\\suppliers\\src\\vendor\\symfony\\property-access\\PropertyAccessor.php:197)"} []