Skip to content

Instantly share code, notes, and snippets.

@melloc01
Last active August 29, 2015 14:12
Show Gist options
  • Save melloc01/1f484d78f40919f8b35a to your computer and use it in GitHub Desktop.
Save melloc01/1f484d78f40919f8b35a to your computer and use it in GitHub Desktop.
<?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