Last active
August 29, 2015 14:12
-
-
Save melloc01/1f484d78f40919f8b35a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function extract($entities ,array $columns = null) | |
{ | |
if (!is_array($entities)) $entities = [ $entities ]; | |
$fieldMappings = \EntityManager::getClassMetadata('Modules\Api\Entity\Product')->fieldMappings; | |
$associationMappings = \EntityManager::getClassMetadata('Modules\Api\Entity\Product')->associationMappings; | |
$extract = []; | |
foreach ($entities as $key => $entity){ | |
//Fields | |
foreach ($fieldMappings as $fieldName => $fieldDefinitions) { | |
$getter = "get{$fieldName}"; | |
if (method_exists($entity, $getter)) | |
$extract[$key][$fieldName] = $entity->$getter(); | |
} | |
//Associations | |
foreach ($associationMappings as $associationName => $associationMapping) { | |
$getter = "get{$associationName}"; | |
if (method_exists($entity, $getter)) | |
$association = $entity->$getter(); | |
switch ($associationMapping['type']) { | |
case \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_ONE: | |
$extract[$key][$associationName] = $association->getId(); | |
break; | |
default: | |
# code... | |
break; | |
} | |
} | |
} | |
return $extract; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment