0

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

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

当我克隆学校对象并打印学校时,它会在克隆中返回正确的对象。但是,当我尝试坚持学校时,它没有正确保存详细信息。我需要做些什么才能使其正常工作吗?我需要设置一个标志吗?

我想做的是:

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

1 回答 1

0

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

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

于 2012-08-13T14:48:01.267 回答