Split elastica->model transformer to orm and mongodb implementations
This commit is contained in:
parent
71314264f4
commit
30eb9c90f5
|
@ -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 -->
|
||||
|
|
|
@ -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);
|
||||
}
|
33
Transformer/ElasticaToModelDoctrineMongoDBTransformer.php
Normal file
33
Transformer/ElasticaToModelDoctrineMongoDBTransformer.php
Normal 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();
|
||||
}
|
||||
}
|
27
Transformer/ElasticaToModelDoctrineORMTransformer.php
Normal file
27
Transformer/ElasticaToModelDoctrineORMTransformer.php
Normal 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.');
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
|
|
Loading…
Reference in a new issue