首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb的doctrine2 odm没有保存克隆的嵌套对象

mongodb的doctrine2 odm没有保存克隆的嵌套对象
EN

Stack Overflow用户
提问于 2012-08-11 00:17:28
回答 1查看 869关注 0票数 0

我有一所学校,每所学校都有多个课时安排,每个课表都有详细信息:

代码语言:javascript
复制
school (document)
-- bell schedule (embedded document)
---- bell schedule details (embedded document)

当我克隆学校对象并print_r学校时,它在克隆中返回适当的对象。然而,当我试图持久化学校时,它并没有正确地保存细节。为了让它正常工作,我需要做些什么吗?有什么我需要设置的标志吗?

我想要做的是:

代码语言:javascript
复制
$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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-13 22:48:01

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11905557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档