我目前正在開發一個語言評估項目,使您能夠以所需語言參加考試並評估您的水平。我使用Symfony2框架並與Doctrine2一起工作。我的問題是以下一種:從Doctrine2和Symfony2獲取單向多對多關係
我有兩個實體考試和問題由多對多關係(考試是所有者)相關聯。每個考試可以涉及幾個問題,每個問題都可以與多個考試相關。
這裏是我的代碼:
考試實體
/**
* Exam
*
* @ORM\Table(name="cids_exam")
* @ORM\Entity(repositoryClass="LA\AdminBundle\Entity\ExamRepository")
*/
class Exam
{
...
/**
* @ORM\ManyToMany(targetEntity="LA\AdminBundle\Entity\Question", cascade={"persist"})
* @ORM\JoinTable(name="cids_exam_question")
*/
private $questions;
...
/**
* Constructor
*/
public function __construct()
{
$this->questions = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add questions
*
* @param \LA\AdminBundle\Entity\Question $questions
* @return Exam
*/
public function addQuestion(\LA\AdminBundle\Entity\Question $questions)
{
$this->questions[] = $questions;
return $this;
}
/**
* Remove questions
*
* @param \LA\AdminBundle\Entity\Question $questions
*/
public function removeQuestion(\LA\AdminBundle\Entity\Question $questions)
{
$this->questions->removeElement($questions);
}
/**
* Get questions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getQuestions()
{
return $this->questions;
}
}
只要它是一個單向的關係,存在於我的問題類中沒有「考試」屬性。
現在,我想要做的是讓所有與特定的考試問題,調用getQuestions()方法,像這樣:
$questions = $exam->getQuestions();
但這種方法即使返回一個空數組,我在我的數據庫中有數據。如果我的var_dump的$考試變量,我能看到的問題數組爲空:
object(LA\AdminBundle\Entity\Exam)[47]
private 'id' => int 5
...
private 'questions' =>
object(Doctrine\ORM\PersistentCollection)[248]
private 'snapshot' =>
array (size=0)
empty
private 'owner' => null
private 'association' => null
private 'em' => null
private 'backRefFieldName' => null
private 'typeClass' => null
private 'isDirty' => boolean false
private 'initialized' => boolean false
private 'coll' =>
object(Doctrine\Common\Collections\ArrayCollection)[249]
private '_elements' =>
array (size=0)
...
我想我可能會寫一findByExam()函數在我QuestionRepository,但我真的不知道如何實現的在這種情況下加入。
任何幫助將是偉大的!
感謝您的偉大的答案!這將有很大幫助。如果我解決了我的問題或遇到了其他問題,我會盡力解決問題;) – Beliasus
我嘗試創建雙向關係而不是單向關係,但仍無法獲取任何內容。你認爲這可能是控制器中的一個問題,就在堅持考試及其相關問題進入數據庫之前?我可能不得不實際將每個問題添加到考試實例?我的意思是你知道如果表單中的綁定請求會調用addQuestion()函數,還是必須手動執行? – Beliasus
找了好幾個小時後,我意識到我的問題根本不是來自多方面的關係,而是來自其他干擾它的事情。實際上,考試實體中的getQuestions()函數是有效的,這是以單向關係做我想做的事情的最佳方式。感謝您的幫助,我使用您的建議添加功能,並且很好地看待了教義獲取。 – Beliasus