Doctrine2 ORM OneToMany或ManyToOne不起作用



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

Survey
id
title
Questions:
id 
survey_id

所以我想列出这些调查及其各自的问题,那么我该如何做到这一点。

这是我的代码,

调查实体:

<?php
namespace SurveyEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use LibraryEntityBaseEntity;

/**
 * Description of Survey
 *
 * @author Mubarak
 */
/**
 * @ORMEntity
 * @ORMTable(name="surveys")
 */
class Survey extends BaseEntity{
    public function __construct() {
        $this->questions = new ArrayCollection();
    }
    /**
     * @ORMColumn(name="title", type="string")
     * @var string
     */
    protected $title;
    /**
     * @ORMColumn(name="description", type="string")
     * @var string
     */
    protected $description;
    /**
     * @ORMOneToMany(targetEntity="SurveyEntityQuestions", 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 SurveyEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use LibraryEntityBaseEntity;

/**
 * Description of Survey Questions
 *
 * @author Mubarak
 */
/**
 * @ORMEntity
 * @ORMTable(name="survey_questions")
 */
class Questions extends BaseEntity{

    /**
     * @ORMColumn(name="question", type="string")
     * @var string
     */
    protected $question;
    /**
     * @ORMManyToOne(targetEntity="SurveyEntitySurvey", inversedBy="questions")
     * @ORMJoinColumn(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()函数返回Questions实体的数组。建议更改为

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

最新更新