manytone逆不工作

lh80um4z  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(365)

我有一种双向的多对一的关系,只在一个方向上起作用,请有人帮忙。为了清晰起见,我试着把这个例子精简到最少的代码。
问题实体可以有许多答案实体。
问题实体

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;
    }
}

当我尝试使用direction 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"
      }
      ...
    ]
  }
  ...
]
ymdaylpp

ymdaylpp1#

看起来还可以。可能只是打字错误。尝试使用 inversedBy="cw_employee_answers" (复数形式为$cw\u employee\u answers)而不是 inversedBy="cw_employee_answer" 应该有用。

相关问题