Doctrine2 ORM OneToMany 或 ManyToOne 不工作

Doctrine2 ORM OneToMany or ManyToOne not working

我想要 2 个表之间的关系,具有 OneToMany 和 ManyToOne 关系。

Survey
id
title

Questions:
id 
survey_id

所以我希望列出调查及其各自的问题,那么我该如何实现呢。

这是我的代码,

调查实体:

<?php

namespace Survey\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;


/**
 * Description of Survey
 *
 * @author Mubarak
 */

/**
 * @ORM\Entity
 * @ORM\Table(name="surveys")
 */

class Survey extends BaseEntity{

    public function __construct() {
        $this->questions = new ArrayCollection();
    }

    /**
     * @ORM\Column(name="title", type="string")
     * @var string
     */
    protected $title;

    /**
     * @ORM\Column(name="description", type="string")
     * @var string
     */
    protected $description;

    /**
     * @ORM\OneToMany(targetEntity="Survey\Entity\Questions", mappedBy="surveys")
     */
    private $questions;

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setDescription($description) {
        $this->description = $description;
        return $this;
    }

    public function getQuestions() {
        return $this->questions;
    }

    public function setQuestions(ArrayCollection $questions) {
        $this->questions = $questions;
    }


    public function __toString() {
        return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
    }
}

下面是问题实体:

<?php

namespace Survey\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;


/**
 * Description of Survey Questions
 *
 * @author Mubarak
 */

/**
 * @ORM\Entity
 * @ORM\Table(name="survey_questions")
 */

class Questions extends BaseEntity{



    /**
     * @ORM\Column(name="question", type="string")
     * @var string
     */
    protected $question;

    /**
     * @ORM\ManyToOne(targetEntity="Survey\Entity\Survey", inversedBy="questions")
     * @ORM\JoinColumn(name="survey_id", referencedColumnName="id")
     */
    private $surveys;

    public function getQuestion() {
        return $this->question;
    }

    public function setQuestion($question) {
        $this->question = $question;
    }

    public function getSurveys() {
        return $this->surveys;
    }

    public function setSurveys(ArrayCollection $surveys) {
        $this->surveys = $surveys;
    }

    public function __toString() {
        return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
    }
}

我在这里做错了什么,这是我获取调查和问题的代码:

$surveysInterface = $this->surveyService->getAllSurveys();
            foreach($surveysInterface as $survey){
                $surveysArray[] = array(
                    'id' => $survey->getId(),
                    'title' => $survey->getTitle(),
                    'description' => $survey->getDescription(),
                    'isActive' => $survey->getActive(),
                    'questions' => array(
                        'question' => $survey->getQuestions()->getQuestion()
                    )
                );
            }
'questions' => array(
                    'question' => $survey->getQuestions()->getQuestion()
                    )

这部分看起来不对,因为 getQuestions() 函数 return Questions 个实体的数组。建议改成

'questions' => $survey->getQuestions()