コード例 #1
0
 /**
  * This method will have a look into the mappings and figure out if the current
  * document has got some mapped common fields and sync them by the mapped sync-type.
  *
  * @param $object
  * @param $referencedObject
  * @param ClassMetadata $classMetadata
  * @throws Exception\MappingException
  */
 private function syncCommonFields($object, $referencedObject, ClassMetadata $classMetadata)
 {
     $referencedObjets = array_filter($classMetadata->getReferencedObjects(), function ($refObject) use($referencedObject) {
         $refl = new \ReflectionClass($refObject['target-object']);
         return $refl->isInstance($referencedObject);
     });
     if (!$referencedObjets || !is_array($referencedObjets)) {
         return;
     }
     // neither a sleeping proxy of the object or referenced objects is good for reflections or the sync process
     if ($this->objectAdapterManager->isSleepingProxy($object) || $this->objectAdapterManager->isSleepingProxy($referencedObject)) {
         return;
     }
     $objectReflection = new \ReflectionClass($object);
     foreach ($referencedObjets as $fieldName => $reference) {
         $commonFieldMappings = $classMetadata->getCommonFields();
         if (!$commonFieldMappings) {
             continue;
         }
         $commonFieldMappings = array_filter($commonFieldMappings, function ($field) use($fieldName) {
             return $field['target-field'] === $fieldName;
         });
         $referencedObjectReflection = new \ReflectionClass($referencedObject);
         foreach ($commonFieldMappings as $commonField) {
             if (!$referencedObjectReflection->hasProperty($commonField['referenced-by'])) {
                 continue;
             }
             $referencedObjectProperty = $referencedObjectReflection->getProperty($commonField['referenced-by']);
             $referencedObjectProperty->setAccessible(true);
             if (!$objectReflection->hasProperty($commonField['inversed-by'])) {
                 continue;
             }
             $objectProperty = $objectReflection->getProperty($commonField['inversed-by']);
             $objectProperty->setAccessible(true);
             if ($commonField['sync-type'] === 'to-reference') {
                 $value = $objectProperty->getValue($object);
                 $referencedObjectProperty->setValue($referencedObject, $value);
             } elseif ($commonField['sync-type'] === 'from-reference') {
                 $value = $referencedObjectProperty->getValue($referencedObject);
                 $objectProperty->setValue($object, $value);
             }
         }
     }
 }