Doctrine2 ORM OneToMany或ManyToOne不起作用

时间:2016-01-13 14:04:18

标签: doctrine-orm zend-framework2

我想在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()
                    )
                );
            }

1 个答案:

答案 0 :(得分:1)

'questions' => array(
                    'question' => $survey->getQuestions()->getQuestion()
                    )

这部分看起来不对,因为getQuestions()函数返回 Questions 实体的数组。建议将其更改为

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