/** * This function is used to convert the array into entity objects. * * @param array $data * * @return array return as entity object array */ public function export($data) { if (empty($data)) { return; } $em = \Env::get('em'); $entityClassMetaData = $em->getClassMetadata($this->entityClass); $repository = $em->getRepository($this->entityClass); $entities = array(); foreach ($data as $entityArray) { $entityObj = null; $primaryKeyName = $entityClassMetaData->getSingleIdentifierFieldName(); if (!empty($entityArray[$primaryKeyName])) { $entityObj = $repository->findOneBy(array($primaryKeyName => $entityArray[$primaryKeyName])); } if (!$entityObj) { $entityObj = new $this->entityClass(); } //get the association mappings $associationMappings = $entityClassMetaData->getAssociationMappings(); //class methods $classMethods = get_class_methods($entityObj); foreach ($entityArray as $entityField => $entityValue) { $associationObj = null; if (!in_array('set' . ucfirst($entityField), $classMethods)) { continue; } if ($entityClassMetaData->isSingleValuedAssociation($entityField)) { $targetEntity = $associationMappings[$entityField]['targetEntity']; $mappingEntityField = $em->getClassMetadata($targetEntity)->getSingleIdentifierFieldName(); //check the association entity $mappingEntityValue = is_array($entityValue) ? isset($entityValue[$mappingEntityField]) ? $entityValue[$mappingEntityField] : 0 : $entityValue; if (!\FWValidator::isEmpty($mappingEntityValue)) { $associationObj = $em->getRepository($targetEntity)->findOneBy(array($mappingEntityField => $mappingEntityValue)); } if (!$associationObj) { $associationObj = new $targetEntity(); } if (is_array($entityValue)) { foreach ($entityValue as $method => $value) { $associationObj->{'set' . ucfirst($method)}($value); } } else { $entityObj->{'set' . ucfirst($entityField)}($associationObj); } $entityValue = $associationObj; } //checks if the string a serialized array if (\FWValidator::is_serialized($entityValue)) { $entityValue = unserialize($entityValue); } $entityObj->{'set' . ucfirst($entityField)}($entityValue); } $entities[] = $entityObj; } return $entities; }