FOSElasticaBundle/Doctrine/ORM/ElasticaToModelTransformer.php

53 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace FOS\ElasticaBundle\Doctrine\ORM;
use FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer;
2011-05-10 01:10:45 +02:00
use Doctrine\ORM\Query;
/**
* 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.
*/
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
{
const ENTITY_ALIAS = 'o';
/**
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
*
* @return array of objects or arrays
*/
protected function findByIdentifiers(array $identifierValues, $hydrate)
{
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;
$qb = $this->getEntityQueryBuilder();
$qb->andWhere($qb->expr()->in(static::ENTITY_ALIAS.'.'.$this->options['identifier'], ':values'))
->setParameter('values', $identifierValues);
2011-05-10 01:10:45 +02:00
return $qb->getQuery()->setHydrationMode($hydrationMode)->execute();
}
/**
2015-03-12 11:20:00 +01:00
* Retrieves a query builder to be used for querying by identifiers.
*
* @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);
}
}