Split elastica->model transformer to orm and mongodb implementations

This commit is contained in:
ornicar 2011-05-09 15:06:55 -07:00
parent 71314264f4
commit 30eb9c90f5
5 changed files with 76 additions and 11 deletions

View file

@ -14,7 +14,7 @@
<argument type="collection" /> <!-- options -->
</service>
<service id="foq_elastica.elastica_to_model_transformer.prototype.mongodb" class="FOQ\ElasticaBundle\Transformer\ElasticaToModelDoctrineTransformer" public="false">
<service id="foq_elastica.elastica_to_model_transformer.prototype.mongodb" class="FOQ\ElasticaBundle\Transformer\ElasticaToModelDoctrineMongoDBTransformer" public="false">
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument /> <!-- model -->
<argument type="collection" /> <!-- options -->

View file

@ -9,7 +9,7 @@ use Elastica_Document;
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids
*/
class ElasticaToModelDoctrineTransformer implements ElasticaToModelTransformerInterface
abstract class ElasticaToModelAbstractDoctrineTransformer implements ElasticaToModelTransformerInterface
{
/**
* Repository to fetch the objects from
@ -60,15 +60,9 @@ class ElasticaToModelDoctrineTransformer implements ElasticaToModelTransformerIn
return $elasticaObject->getId();
}, $elasticaObjects);
$objects = $this->objectManager
->createQueryBuilder($this->objectClass)
->field($this->options['identifier'])->in($ids)
->hydrate($this->options['hydrate'])
->getQuery()
->execute()
->toArray();
$objects = $this->findByIdentifiers($this->objectClass, $this->options['identifier'], $ids, $this->options['hydrate']);
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
// sort objects in the order of ids
$idPos = array_flip($ids);
@ -79,4 +73,15 @@ class ElasticaToModelDoctrineTransformer implements ElasticaToModelTransformerIn
return $objects;
}
/**
* Fetches objects by theses identifier values
*
* @param string $class the model class
* @param string $identifierField like 'id'
* @param array $identifierValues ids values
* @param mixed $hydrate whether or not to hydrate the objects, false returns arrays
* @return array of objects or arrays
*/
protected abstract function findByIdentifiers($class, $identifierField, array $identifierValues, $hydrate);
}

View file

@ -0,0 +1,33 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
use Elastica_Document;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids
*/
class ElasticaToModelDoctrineMongoDBTransformer extends ElasticaToModelAbstractDoctrineTransformer
{
/**
* Fetch objects for theses identifier values
*
* @param string $class the model class
* @param string $identifierField like 'id'
* @param array $identifierValues ids values
* @param mixed $hydrate whether or not to hydrate the objects, false returns arrays
* @return array of objects or arrays
*/
protected function findByIdentifiers($class, $identifierField, array $identifierValues, $hydrate)
{
return $this->objectManager
->createQueryBuilder($class)
->field($identifierField)->in($identifierValues)
->hydrate($hydrate)
->getQuery()
->execute()
->toArray();
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
use Elastica_Document;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids
*/
class ElasticaToModelDoctrineORMTransformer extends ElasticaToModelAbstractDoctrineTransformer
{
/**
* Fetch objects for theses identifier values
*
* @param string $class the model class
* @param string $identifierField like 'id'
* @param array $identifierValues ids values
* @param mixed $hydrate whether or not to hydrate the objects, false returns arrays
* @return array of objects or arrays
*/
protected function findByIdentifiers($class, $identifierField, array $identifierValues, $hydrate)
{
throw new \Exception('Not implemented yet. Implement me! See ElasticaToModelDoctrineMongoDBTransformer as an example.');
}
}

View file

@ -50,7 +50,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
}
$array[$key] = $this->normalizeValue($object->$getter());
}
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
$identifier = $object->$identifierGetter();
return new Elastica_Document($identifier, array_filter($array));