so i have a school, each school has multiple bell schedules and each schedule has details:
school (document)
-- bell schedule (embedded document)
---- bell schedule details (embedded document)
when i clone the school object and print_r the school, it comes back with the proper objects in the clone. however, when i try to persist the school, it doesn’t save the details properly. is there something that I need to do in order for this to work properly? is there a flag I need to set or something?
what im trying to do is:
$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;
}
Your
@EmbedManyannotations are missing thetargetDocumentattribute, which should correspond to the class name of the embedded object(s). See the annotation reference for more information. Also, you are missing field mappings for the BellScheduleDetail class. You may want to use @String for those fields.Lastly, I would advise initializing your EmbedMany and ReferenceMany fields as ArrayCollection instances rather than empty arrays. That way you can always expect the property to be a Collection implementation (either ArrayCollection or PersistentCollection). It may not make much difference in your case (using the
[]operator), but it can come in handy if you find yourself doing other array operations on the property.