Merge pull request #113 from Fran6co/fix-propertypath
Using PropertyPath for resolving what getter to use
This commit is contained in:
commit
765d875d86
|
@ -5,6 +5,7 @@ namespace FOQ\ElasticaBundle\Doctrine;
|
|||
use FOQ\ElasticaBundle\HybridResult;
|
||||
use FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
||||
use Elastica_Document;
|
||||
use Symfony\Component\Form\Util\PropertyPath;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with Doctrine objects
|
||||
|
@ -76,13 +77,13 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
|
|||
throw new \RuntimeException('Cannot find corresponding Doctrine objects for all Elastica results.');
|
||||
};
|
||||
|
||||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
$identifierProperty = new PropertyPath($this->options['identifier']);
|
||||
|
||||
// sort objects in the order of ids
|
||||
$idPos = array_flip($ids);
|
||||
usort($objects, function($a, $b) use ($idPos, $identifierGetter)
|
||||
usort($objects, function($a, $b) use ($idPos, $identifierProperty)
|
||||
{
|
||||
return $idPos[$a->$identifierGetter()] > $idPos[$b->$identifierGetter()];
|
||||
return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)];
|
||||
});
|
||||
|
||||
return $objects;
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace FOQ\ElasticaBundle\Propel;
|
|||
use FOQ\ElasticaBundle\HybridResult;
|
||||
use FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
||||
use Elastica_Document;
|
||||
use Symfony\Component\Form\Util\PropertyPath;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with Propel objects
|
||||
|
@ -59,17 +60,17 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
|
|||
|
||||
$objects = $this->findByIdentifiers($this->objectClass, $this->options['identifier'], $ids, $this->options['hydrate']);
|
||||
|
||||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
$identifierProperty = new PropertyPath($this->options['identifier']);
|
||||
|
||||
// sort objects in the order of ids
|
||||
$idPos = array_flip($ids);
|
||||
if (is_object($objects)) {
|
||||
$objects->uasort(function($a, $b) use ($idPos, $identifierGetter) {
|
||||
return $idPos[$a->$identifierGetter()] > $idPos[$b->$identifierGetter()];
|
||||
$objects->uasort(function($a, $b) use ($idPos, $identifierProperty) {
|
||||
return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)];
|
||||
});
|
||||
} else {
|
||||
usort($objects, function($a, $b) use ($idPos, $identifierGetter) {
|
||||
return $idPos[$a->$identifierGetter()] > $idPos[$b->$identifierGetter()];
|
||||
usort($objects, function($a, $b) use ($idPos, $identifierProperty) {
|
||||
return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)];
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
use FOQ\ElasticaBundle\HybridResult;
|
||||
use Symfony\Component\Form\Util\PropertyPath;
|
||||
|
||||
/**
|
||||
* Holds a collection of transformers for an index wide transformation.
|
||||
|
@ -36,12 +37,12 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
|
|||
$sorted[$object->getType()][] = $object;
|
||||
}
|
||||
|
||||
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
|
||||
$identifierProperty = new PropertyPath($this->options['identifier']);
|
||||
|
||||
$transformed = array();
|
||||
foreach ($sorted AS $type => $objects) {
|
||||
$transformedObjects = $this->transformers[$type]->transform($objects);
|
||||
$transformed[$type] = array_combine(array_map(function($o) use ($identifierGetter) {return $o->$identifierGetter();},$transformedObjects),$transformedObjects);
|
||||
$transformed[$type] = array_combine(array_map(function($o) use ($identifierProperty) {return $identifierProperty->getValue($o);},$transformedObjects),$transformedObjects);
|
||||
}
|
||||
|
||||
$result = array();
|
||||
|
|
|
@ -6,6 +6,7 @@ use Elastica_Document;
|
|||
use Traversable;
|
||||
use ArrayAccess;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Form\Util\PropertyPath;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with Doctrine objects
|
||||
|
@ -44,14 +45,11 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
|
|||
{
|
||||
$array = array();
|
||||
foreach ($fields as $key) {
|
||||
$getter = 'get'.ucfirst($key);
|
||||
if (!is_callable(array($object, $getter))) {
|
||||
throw new RuntimeException(sprintf('The method %s::%s is not callable', get_class($object), $getter));
|
||||
}
|
||||
$array[$key] = $this->normalizeValue($object->$getter());
|
||||
$property = new PropertyPath($key);
|
||||
$array[$key] = $this->normalizeValue($property->getValue($object));
|
||||
}
|
||||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
$identifier = $object->$identifierGetter();
|
||||
$identifierProperty = new PropertyPath($this->options['identifier']);
|
||||
$identifier = $identifierProperty->getValue($object);
|
||||
|
||||
return new Elastica_Document($identifier, $array);
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
"php": ">=5.3.2",
|
||||
"symfony/framework-bundle": "2.1.*",
|
||||
"symfony/console": "2.1.*",
|
||||
"symfony/form": "2.1.*",
|
||||
"ruflin/elastica": ">=0.19.3"
|
||||
},
|
||||
"suggest": {
|
||||
|
|
Loading…
Reference in a new issue