/**
  * @see PersistableInterface
  *
  * @return array
  */
 public function preparePersistChangeSet()
 {
     $changeSet = array('name' => $this->name);
     if ($this->address !== null) {
         $changeSet['address1'] = $this->address->getAddress1();
         $changeSet['address2'] = $this->address->getAddress2();
         $changeSet['city'] = $this->address->getCity();
         $changeSet['state'] = $this->address->getState();
         $changeSet['zip'] = $this->address->getZip();
     }
     if ($this->id !== null) {
         $changeSet['_id'] = (int) $this->id;
     }
     return $changeSet;
 }
 /**
  * @see HydratableInterface
  *
  * @param array                                           $data
  * @param \Doctrine\SkeletonMapper\ObjectManagerInterface $objectManager
  */
 public function hydrate(array $data, ObjectManagerInterface $objectManager)
 {
     if (isset($data['_id'])) {
         $this->id = (int) $data['_id'];
     }
     if (isset($data['name'])) {
         $this->name = (string) $data['name'];
     }
     if (isset($data['address1']) || isset($data['address2']) || isset($data['city']) || isset($data['state']) || isset($data['zip'])) {
         $profile = $this;
         $this->address = function () use($data, $profile) {
             $address = new Address($profile);
             if (isset($data['address1'])) {
                 $address->setAddress1($data['address1']);
             }
             if (isset($data['address2'])) {
                 $address->setAddress2($data['address2']);
             }
             if (isset($data['city'])) {
                 $address->setCity($data['city']);
             }
             if (isset($data['state'])) {
                 $address->setState($data['state']);
             }
             if (isset($data['zip'])) {
                 $address->setZip($data['zip']);
             }
             return $address;
         };
     }
 }
 public function testEmbeddedAddress()
 {
     $user = $this->objectManager->find($this->userClassName, 1);
     $profile = new Profile();
     $address = new Address($profile);
     $address->setAddress1('273 Lake Terrace Dr.');
     $address->setCity('Hendersonville');
     $address->setState('TN');
     $address->setZip('37075');
     $profile->setAddress($address);
     $profile->setName('Jonathan H. Wage');
     $user->setProfile($profile);
     $this->objectManager->persist($profile);
     $this->objectManager->flush();
     $this->objectManager->clear();
     $user = $this->objectManager->find($this->userClassName, 1);
     $this->assertNotNull($user->getProfile()->getAddress());
     $this->assertEquals($address, $user->getProfile()->getAddress());
     $user->getProfile()->getAddress()->setState('Tennessee');
     $this->objectManager->flush();
     $this->objectManager->clear();
     $user = $this->objectManager->find($this->userClassName, 1);
     $this->assertEquals('Tennessee', $user->getProfile()->getAddress()->getState());
 }