2011-05-10 00:06:55 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Doctrine\ORM;
|
2011-05-10 00:06:55 +02:00
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer;
|
2011-05-10 01:10:45 +02:00
|
|
|
use Doctrine\ORM\Query;
|
2011-05-10 00:06:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps Elastica documents with Doctrine objects
|
|
|
|
* This mapper assumes an exact match between
|
2015-03-12 11:20:00 +01:00
|
|
|
* elastica documents ids and doctrine object ids.
|
2011-05-10 00:06:55 +02:00
|
|
|
*/
|
2011-05-10 00:31:20 +02:00
|
|
|
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
|
2011-05-10 00:06:55 +02:00
|
|
|
{
|
2013-09-23 01:57:58 +02:00
|
|
|
const ENTITY_ALIAS = 'o';
|
|
|
|
|
2011-05-10 00:06:55 +02:00
|
|
|
/**
|
2015-03-12 11:20:00 +01:00
|
|
|
* Fetch objects for theses identifier values.
|
|
|
|
*
|
|
|
|
* @param array $identifierValues ids values
|
|
|
|
* @param Boolean $hydrate whether or not to hydrate the objects, false returns arrays
|
2011-05-10 00:06:55 +02:00
|
|
|
*
|
|
|
|
* @return array of objects or arrays
|
|
|
|
*/
|
2012-07-10 22:28:31 +02:00
|
|
|
protected function findByIdentifiers(array $identifierValues, $hydrate)
|
2011-05-10 00:06:55 +02:00
|
|
|
{
|
2011-05-15 03:54:35 +02:00
|
|
|
if (empty($identifierValues)) {
|
|
|
|
return array();
|
|
|
|
}
|
2011-05-15 04:19:38 +02:00
|
|
|
$hydrationMode = $hydrate ? Query::HYDRATE_OBJECT : Query::HYDRATE_ARRAY;
|
2013-09-23 01:57:58 +02:00
|
|
|
|
|
|
|
$qb = $this->getEntityQueryBuilder();
|
2014-05-08 01:26:30 +02:00
|
|
|
$qb->andWhere($qb->expr()->in(static::ENTITY_ALIAS.'.'.$this->options['identifier'], ':values'))
|
2011-06-20 21:10:55 +02:00
|
|
|
->setParameter('values', $identifierValues);
|
2011-05-10 01:10:45 +02:00
|
|
|
|
|
|
|
return $qb->getQuery()->setHydrationMode($hydrationMode)->execute();
|
2011-05-10 00:06:55 +02:00
|
|
|
}
|
2013-09-23 01:57:58 +02:00
|
|
|
|
|
|
|
/**
|
2015-03-12 11:20:00 +01:00
|
|
|
* Retrieves a query builder to be used for querying by identifiers.
|
2013-09-23 01:57:58 +02:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\QueryBuilder
|
|
|
|
*/
|
|
|
|
protected function getEntityQueryBuilder()
|
|
|
|
{
|
|
|
|
$repository = $this->registry
|
|
|
|
->getManagerForClass($this->objectClass)
|
|
|
|
->getRepository($this->objectClass);
|
|
|
|
|
|
|
|
return $repository->{$this->options['query_builder_method']}(static::ENTITY_ALIAS);
|
|
|
|
}
|
2011-05-10 00:06:55 +02:00
|
|
|
}
|