【问题标题】:doctrine2 odm for mongodb isn't saving the clone's nested objectsmongodb 的教义2 odm 没有保存克隆的嵌套对象
【发布时间】:2012-08-10 16:17:28
【问题描述】:

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

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

【问题讨论】:

  • 你能分享你的类映射(例如注解、XML、YML)吗?此外,如果需要转储托管对象,您可能会发现 \Doctrine\Common\Util\Debug::dump() 函数很有用,因为它忽略了对内部 Doctrine 服务的引用,这些服务很容易混淆正常的 var_dump()print_r() 输出。

标签: php mongodb doctrine-orm


【解决方案1】:

您的@EmbedMany 注释缺少targetDocument 属性,该属性应对应于嵌入对象的类名。请参阅annotation reference 了解更多信息。此外,您缺少 BellScheduleDetail 类的字段映射。您可能希望对这些字段使用 @String

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

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 2017-06-26
    • 2017-02-22
    • 1970-01-01
    • 2021-08-13
    • 2018-06-30
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多