Doctrine2 MongoDB的ODM不会保存克隆的嵌套对象


doctrine2 odm for mongodb isn't saving the clone's nested objects

所以我有一所学校,每所学校都有多个钟表,每个时间表都有详细信息:

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;
    }

您的@EmbedMany批注缺少 targetDocument 属性,该属性应与嵌入对象的类名相对应。有关详细信息,请参阅注释参考。此外,您还缺少 BellScheduleDetail 类的字段映射。您可能希望对这些字段使用@String。

最后,我建议将 EmbedMany 和 ReferenceMany 字段初始化为 ArrayCollection 实例,而不是空数组。这样,您始终可以期望该属性是集合实现(ArrayCollection 或 PersistentCollection)。在您的情况下(使用 [] 运算符)可能没有太大区别,但如果您发现自己在属性上执行其他数组操作,它会派上用场。