У меня двусторонние отношения «многие-один-один-многие», которые работают только в одном направлении, может ли кто-нибудь помочь. Я попытался сократить пример до минимального кода, необходимого для ясности.
Сущность вопроса может иметь много сущностей ответа.
Сущность вопроса
class CwEmployeeQuestion
{
public function __construct()
{
$this->cw_employee_answers = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @ORM\Column(type = "guid")
* @ORM\Id
* @ORM\GeneratedValue(strategy = "UUID")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity = "Fitchek\Entity\Corporate\CwEmployeeAnswer", mappedBy = "cw_employee_question")
*/
protected $cw_employee_answers;
/**
* Get CwEmployeeAnswer
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCwEmployeeAnswer()
{
return $this->cw_employee_answers;
}
}
Ответить
class CwEmployeeAnswer
{
/**
* @ORM\Column(type = "guid")
* @ORM\Id
* @ORM\GeneratedValue(strategy = "UUID")
*/
protected $id;
/**
* Many CwEmployeeAnswer have One CwEmployeeQuestion.
*
* @var \Fitchek\Entity\Corporate\CwEmployeeQuestion $cw_employee_question
*
* @ORM\ManyToOne(targetEntity = "Fitchek\Entity\Corporate\CwEmployeeQuestion", inversedBy = "cw_employee_answer")
* @ORM\JoinColumn(name = "cw_employee_question", referencedColumnName = "id")
*/
protected $cw_employee_question;
/**
* Set $cw_employee_question
*
* @param \Fitchek\Entity\Corporate\CwEmployeeQuestion $cw_employee_question
* @return CwEmployeeAnswer
*/
public function setCwEmployeeQuestion(?\Fitchek\Entity\Corporate\CwEmployeeQuestion $cw_employee_question = null): self
{
$this->cw_employee_question = $cw_employee_question;
return $this;
}
/**
* Get $cw_employee_question
*
* @return \Fitchek\Entity\Corporate\CwEmployeeQuestion
*/
public function getCwEmployeeQuestion(): ?CwEmployeeQuestion
{
return $this->cw_employee_question;
}
/**
*
* {@inheritdoc}
*
* @param JSON $json, the request JSON to create a new CwEmployeeAnswer
* @return CwEmployeeAnswer the newly created CwEmployeeAnswer
*/
public function createFromData($json, $cw_employee_question = null)
{
$this->setCwEmployeeQuestion($cw_employee_question);
return $this;
}
}
Когда я пробую направление answer-> question, он работает нормально и возвращает мне список ответов с соответствующим вопросом как часть JSON. Например:
[
{ "id": 1
"name": "answer 1",
"question" : {
"name": "question 1"
}
}
...
]
Однако при запросе в другом направлении, чтобы получить все вопросы с их ответами как часть возвращаемого JSON, массив вопросов возвращается как null. Например:
[
{ "id": 1
"name": "question 1",
"answers": null
}
...
]
Когда я хотел бы увидеть:
[
{ "id": 1
"name": "question 1",
"answers" : [
{
"name": "answer 1"
},
{
"name": "answer 2"
}
...
]
}
...
]






Вроде нормально. Наверное, опечатка.
Попробуйте использовать inversedBy = "cw_employee_answers" (множественное число как $ cw_employee_answers) вместо inversedBy = "cw_employee_answer"
Он должен работать.