2012-08-10 66 views
0

,所以我有一所學校,每所學校都有多鍾時間安排和每個計劃有更多的細節:MongoDB的doctrine2 ODM不保存克隆的嵌套對象

school (document) 
-- bell schedule (embedded document) 
---- bell schedule details (embedded document) 

當我克隆學校對象和print_r的學校,它會返回克隆中正確的對象。然而,當我嘗試堅持學校時,它並沒有妥善保存細節。有什麼我需要做的,爲了這個正常工作?有沒有我需要設置的標誌或什麼?

什麼即時試圖做的是:

$school2 = clone $school1; 
$dm->persist($school2); 
$dm->flush(); 

---- classes ---- 

    /** 
    * @MongoDB\Document(collection="schools") 
    */ 
    class School 
    { 
     /** 
     * @MongoDB\EmbedMany 
     */ 
     protected $bell_schedules = array(); 

     public function addBellSchedules(BellSchedule $bellSchedules) 
     { 
      $this->bell_schedules[] = $bellSchedules; 
     } 

     public function getBellSchedules() 
     { 
      return $this->bell_schedules; 
     } 

     public function setBellSchedules(\Doctrine\Common\Collections\ArrayCollection $bell_schedules) 
     { 
      $this->bell_schedules = $bell_schedules; 
      return $this; 
     } 
    } 


    /** 
    * @MongoDB\EmbeddedDocument 
    */ 
    class BellSchedule 
    { 
     /** 
     * @MongoDB\EmbedMany 
     */ 
     private $bell_schedule_details 

     public function getBellScheduleDetails() 
     { 
      return $this->bell_schedule_details; 
     } 

     public function setBellScheduleDetails(\Doctrine\Common\Collections\ArrayCollection $bell_schedule_details) 
     { 
      $this->bell_schedule_details = $bell_schedule_details; 
      return $this; 
     } 
    } 

    /** 
    * @MongoDB\EmbeddedDocument 
    */ 
    class BellScheduleDetail 
    {  
     private $period; 
     private $label; 
    } 
+1

你能分享你的類映射(例如註釋,XML,YML)嗎?另外,如果需要轉儲託管對象,您可能會發現'\ Doctrine \ Common \ Util \ Debug :: dump()'函數很有用,因爲它忽略了對內部Doctrine服務的引用,這些服務很容易混淆正常的'var_dump()'和'print_r()'輸出。 – jmikola 2012-08-10 16:30:01

回答

0

@EmbedMany註釋缺少targetDocument屬性,它應該對應於嵌入的對象(S)的類名。請參閱annotation reference瞭解更多信息。此外,您還缺少BellScheduleDetail類的字段映射。您可能需要在這些字段中使用@String

最後,我建議初始化您的EmbedMany和ReferenceMany字段爲ArrayCollection實例而不是空數組。這樣,您可以始終期望該屬性是一個Collection實現(ArrayCollection或PersistentCollection)。它可能沒有太大區別(使用[]運算符),但如果您發現自己正在對屬性執行其他陣列操作,它可能會派上用場。