/**
  * @inheritdoc
  */
 function hydrate($document, $data, array $hints = [])
 {
     $hydratedData = [];
     foreach ($this->metadata->fieldMappings as $fieldName => $mapping) {
         $name = $mapping['name'];
         $propertyValue = isset($data[$name]) ? $data[$name] : null;
         if (!isset($mapping['association'])) {
             if ($propertyValue === null) {
                 continue;
             }
             $type = Type::getType($mapping['type']);
             $value = $type->convertToPHPValue($propertyValue);
             $this->metadata->reflFields[$fieldName]->setValue($document, $value);
             $hydratedData[$fieldName] = $value;
             continue;
         }
         if ($mapping['association'] & ClassMetadata::TO_MANY) {
             $coll = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow);
             $coll->setOwner($document, $mapping);
             $coll->setInitialized(false);
             if ($propertyValue) {
                 $coll->setData($propertyValue);
             }
             $this->metadata->reflFields[$fieldName]->setValue($document, $coll);
             $hydratedData[$fieldName] = $coll;
             continue;
         }
         if ($propertyValue === null) {
             continue;
         }
         if ($mapping['association'] === ClassMetadata::LINK) {
             if (is_string($propertyValue)) {
                 $link = $this->dm->getReference($propertyValue);
             } else {
                 $link = $this->uow->getOrCreateDocument($propertyValue);
             }
             $this->metadata->reflFields[$fieldName]->setValue($document, $link);
             $hydratedData[$fieldName] = $link;
             continue;
         }
         if ($mapping['association'] === ClassMetadata::EMBED) {
             // an embed one must have @class, we would support generic JSON properties via another mapping type
             if (!isset($propertyValue[self::ORIENT_PROPERTY_CLASS])) {
                 throw new HydratorException(sprintf("missing @class for embedded property '%s'", $name));
             }
             $oclass = $propertyValue[self::ORIENT_PROPERTY_CLASS];
             $embeddedMetadata = $this->dm->getMetadataFactory()->getMetadataForOClass($oclass);
             $doc = $embeddedMetadata->newInstance();
             $embeddedData = $this->dm->getHydratorFactory()->hydrate($doc, $propertyValue, $hints);
             $this->uow->registerManaged($doc, null, $embeddedData);
             $this->metadata->reflFields[$fieldName]->setValue($document, $doc);
             $hydratedData[$fieldName] = $doc;
             continue;
         }
     }
     return $hydratedData;
 }
 private function loadIndirectReferenceCollection(PersistentCollection $collection)
 {
     $rows = $collection->getData();
     if (count($rows) === 0) {
         return;
     }
     $mapping = $collection->getMapping();
     $prop = $mapping['direction'] === 'in' ? 'out' : 'in';
     $rids = [];
     $results = [];
     $edgeRids = [];
     foreach ($rows as $row) {
         if (is_string($row)) {
             // edge RID
             $edgeRids[] = $row;
             continue;
         }
         // edge is loaded, so we
         $edgeRid = $row['@rid'];
         if (is_string($row[$prop])) {
             $rids[$row[$prop]][] = $edgeRid;
         } else {
             $results[$edgeRid] = $rows[$prop];
         }
     }
     // load edges and their immediate children (*:1)
     if ($edgeRids) {
         $loaded = $this->binding->query(sprintf('SELECT FROM [%s]', implode(',', $edgeRids)), -1, '*:1');
         $results = array_merge($results, self::extractVertexes($loaded, $prop));
     }
     if ($rids) {
         $loaded = $this->binding->query(sprintf('SELECT FROM [%s]', implode(',', array_keys($rids))));
         foreach ($loaded as $row) {
             $rid = $row['@rid'];
             foreach ($rids[$rid] as $edge) {
                 $results[$edge] = $row;
             }
         }
     }
     foreach ($results as $key => $data) {
         $document = $this->uow->getOrCreateDocument($data);
         $collection->set($key, $document);
     }
 }
Example #3
0
 /**
  * Get a documents actual data, flattening all the objects to arrays.
  *
  * @param object $document
  *
  * @return array
  */
 public function getDocumentActualData($document)
 {
     $class = $this->dm->getClassMetadata(get_class($document));
     $actualData = [];
     foreach ($class->fieldMappings as $fieldName => $mapping) {
         if (isset($mapping['notSaved'])) {
             continue;
         }
         $rp = $class->reflFields[$fieldName];
         $value = $rp->getValue($document);
         if (isset($mapping['association']) && $mapping['association'] & ClassMetadata::TO_MANY && $value !== null && !$value instanceof PersistentCollection) {
             // If $actualData[$name] is not a Collection then use an ArrayCollection.
             if (!$value instanceof Collection) {
                 $value = new ArrayCollection($value);
             }
             // Inject PersistentCollection
             $coll = new PersistentCollection($value, $this->dm, $this);
             $coll->setOwner($document, $mapping);
             $coll->setDirty(!$value->isEmpty());
             $rp->setValue($document, $coll);
             $value = $coll;
         }
         $actualData[$fieldName] = $value;
     }
     return $actualData;
 }