Introduce builtin doctrine provider and finder
This commit is contained in:
parent
0dd0b395f1
commit
eba893495d
|
@ -86,6 +86,30 @@ class Configuration
|
||||||
->useAttributeAsKey('name')
|
->useAttributeAsKey('name')
|
||||||
->prototype('array')
|
->prototype('array')
|
||||||
->treatNullLike(array())
|
->treatNullLike(array())
|
||||||
|
->children()
|
||||||
|
->arrayNode('doctrine')
|
||||||
|
->addDefaultsIfNotSet()
|
||||||
|
->children()
|
||||||
|
->scalarNode('driver')->end()
|
||||||
|
->scalarNode('model')->end()
|
||||||
|
->scalarNode('identifier')->defaultValue('id')->end()
|
||||||
|
->arrayNode('provider')
|
||||||
|
->addDefaultsIfNotSet()
|
||||||
|
->children()
|
||||||
|
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
|
||||||
|
->scalarNode('batch_size')->defaultValue(100)->end()
|
||||||
|
->scalarNode('clear_object_manager')->defaultTrue()->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->arrayNode('finder')
|
||||||
|
->addDefaultsIfNotSet()
|
||||||
|
->children()
|
||||||
|
->scalarNode('hydrate')->defaultTrue()->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
->append($this->getMappingsNode())
|
->append($this->getMappingsNode())
|
||||||
->end()
|
->end()
|
||||||
;
|
;
|
||||||
|
|
|
@ -7,24 +7,26 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Definition;
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
|
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
|
||||||
class FOQElasticaExtension extends Extension
|
class FOQElasticaExtension extends Extension
|
||||||
{
|
{
|
||||||
|
protected $supportedProviderDrivers = array('mongodb');
|
||||||
protected $typeMappings = array();
|
protected $typeMappings = array();
|
||||||
|
protected $loadedDoctrineDrivers = array();
|
||||||
|
|
||||||
public function load(array $configs, ContainerBuilder $container)
|
public function load(array $configs, ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
|
||||||
$loader->load('config.xml');
|
|
||||||
|
|
||||||
$configuration = new Configuration();
|
$configuration = new Configuration();
|
||||||
$processor = new Processor();
|
$processor = new Processor();
|
||||||
|
|
||||||
$config = $processor->process($configuration->getConfigTree(), $configs);
|
$config = $processor->process($configuration->getConfigTree(), $configs);
|
||||||
|
|
||||||
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||||
|
$loader->load('config.xml');
|
||||||
|
|
||||||
if (empty($config['clients']) || empty($config['indexes'])) {
|
if (empty($config['clients']) || empty($config['indexes'])) {
|
||||||
throw new InvalidArgumentException('You must define at least one client and one index');
|
throw new InvalidArgumentException('You must define at least one client and one index');
|
||||||
}
|
}
|
||||||
|
@ -101,7 +103,7 @@ class FOQElasticaExtension extends Extension
|
||||||
$indexDef->setFactoryService($clientId);
|
$indexDef->setFactoryService($clientId);
|
||||||
$indexDef->setFactoryMethod('getIndex');
|
$indexDef->setFactoryMethod('getIndex');
|
||||||
$container->setDefinition($indexId, $indexDef);
|
$container->setDefinition($indexId, $indexDef);
|
||||||
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $indexId);
|
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId);
|
||||||
$indexIds[$name] = $indexId;
|
$indexIds[$name] = $indexId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +116,7 @@ class FOQElasticaExtension extends Extension
|
||||||
* @param array $config An array of types configurations
|
* @param array $config An array of types configurations
|
||||||
* @param ContainerBuilder $container A ContainerBuilder instance
|
* @param ContainerBuilder $container A ContainerBuilder instance
|
||||||
*/
|
*/
|
||||||
protected function loadTypes(array $types, ContainerBuilder $container, $indexId)
|
protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId)
|
||||||
{
|
{
|
||||||
foreach ($types as $name => $type) {
|
foreach ($types as $name => $type) {
|
||||||
$typeId = sprintf('%s.%s', $indexId, $name);
|
$typeId = sprintf('%s.%s', $indexId, $name);
|
||||||
|
@ -129,6 +131,51 @@ class FOQElasticaExtension extends Extension
|
||||||
$type['mappings']
|
$type['mappings']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (isset($type['doctrine'])) {
|
||||||
|
$this->loadTypeDoctrineIntegration($type['doctrine'], $container, $typeDef, $indexName, $name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the optional provider and finder for a type
|
||||||
|
*
|
||||||
|
* @return null
|
||||||
|
**/
|
||||||
|
public function loadTypeDoctrineIntegration(array $config, ContainerBuilder $container, Definition $typeDef, $indexName, $typeName)
|
||||||
|
{
|
||||||
|
if (!in_array($config['driver'], $this->supportedProviderDrivers)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('The provider driver "%s" is not supported'));
|
||||||
|
}
|
||||||
|
$this->loadDoctrineDriver($container, $config['driver']);
|
||||||
|
if (isset($config['provider'])) {
|
||||||
|
$abstractProviderId = sprintf('foq_elastica.provider.prototype.%s', $config['driver']);
|
||||||
|
$providerId = sprintf('foq_elastica.provider.%s.%s', $indexName, $typeName);
|
||||||
|
$providerDef = new DefinitionDecorator($abstractProviderId);
|
||||||
|
$providerDef->setArgument(0, $typeDef);
|
||||||
|
$providerDef->setArgument(3, $config['model']);
|
||||||
|
$providerDef->setArgument(4, array_merge($config['provider'], array(
|
||||||
|
'identifier' => $config['identifier']
|
||||||
|
)));
|
||||||
|
$container->setDefinition($providerId, $providerDef);
|
||||||
|
$container->getDefinition('foq_elastica.populator')
|
||||||
|
->addMethodCall('addProvider', array($providerId, new Reference($providerId)));
|
||||||
|
}
|
||||||
|
if (isset($config['finder'])) {
|
||||||
|
$abstractMapperId = sprintf('foq_elastica.mapper.prototype.%s', $config['driver']);
|
||||||
|
$mapperId = sprintf('foq_elastica.mapper.%s.%s', $indexName, $typeName);
|
||||||
|
$mapperDef = new DefinitionDecorator($abstractMapperId);
|
||||||
|
$mapperDef->setArgument(1, $config['model']);
|
||||||
|
$mapperDef->setArgument(2, array_merge($config['finder'], array(
|
||||||
|
'identifier' => $config['identifier']
|
||||||
|
)));
|
||||||
|
$container->setDefinition($mapperId, $mapperDef);
|
||||||
|
$abstractFinderId = 'foq_elastica.finder.prototype';
|
||||||
|
$finderId = sprintf('foq_elastica.finder.%s.%s', $indexName, $typeName);
|
||||||
|
$finderDef = new DefinitionDecorator($abstractFinderId);
|
||||||
|
$finderDef->setArgument(0, $typeDef);
|
||||||
|
$finderDef->setArgument(1, new Reference($mapperId));
|
||||||
|
$container->setDefinition($finderId, $finderDef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,4 +201,14 @@ class FOQElasticaExtension extends Extension
|
||||||
$managerDef = $container->getDefinition('foq_elastica.mapping_setter');
|
$managerDef = $container->getDefinition('foq_elastica.mapping_setter');
|
||||||
$managerDef->setArgument(0, $mappings);
|
$managerDef->setArgument(0, $mappings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function loadDoctrineDriver(ContainerBuilder $container, $driver)
|
||||||
|
{
|
||||||
|
if (in_array($driver, $this->loadedDoctrineDrivers)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||||
|
$loader->load($driver.'.xml');
|
||||||
|
$this->loadedDoctrineDrivers[] = $driver;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
15
Finder/FinderInterface.php
Normal file
15
Finder/FinderInterface.php
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FOQ\ElasticaBundle\Finder;
|
||||||
|
|
||||||
|
interface FinderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Searches for query results within a given limit
|
||||||
|
*
|
||||||
|
* @param mixed $query Can be a string, an array or an Elastica_Query object
|
||||||
|
* @param int $limit How many results to get
|
||||||
|
* @return array results
|
||||||
|
*/
|
||||||
|
function find($query, $limit);
|
||||||
|
}
|
63
Finder/MappedFinder.php
Normal file
63
Finder/MappedFinder.php
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FOQ\ElasticaBundle\Finder;
|
||||||
|
|
||||||
|
use FOQ\ElasticaBundle\MapperInterface;
|
||||||
|
use FOQ\ElasticaBundle\Paginator\DoctrinePaginatorAdapter;
|
||||||
|
use Zend\Paginator\Paginator;
|
||||||
|
use Elastica_Searchable;
|
||||||
|
use Elastica_Query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds elastica documents and map them to persisted objects
|
||||||
|
*/
|
||||||
|
class MappedFinder implements FinderInterface, PaginatedFinderInterface
|
||||||
|
{
|
||||||
|
protected $searchable;
|
||||||
|
protected $mapper;
|
||||||
|
|
||||||
|
public function __construct(Elastica_Searchable $searchable, MapperInterface $mapper)
|
||||||
|
{
|
||||||
|
$this->searchable = $searchable;
|
||||||
|
$this->mapper = $mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a query string in the food searchable
|
||||||
|
*
|
||||||
|
* @return array of Food documents
|
||||||
|
**/
|
||||||
|
public function find($query, $limit)
|
||||||
|
{
|
||||||
|
$queryObject = Elastica_Query::create($query);
|
||||||
|
$queryObject->setLimit($limit);
|
||||||
|
$results = $this->searchable->search($queryObject)->getResults();
|
||||||
|
|
||||||
|
return $this->mapper->fromElasticaObjects($results);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a paginator wrapping the result of a search
|
||||||
|
*
|
||||||
|
* @return Paginator
|
||||||
|
**/
|
||||||
|
public function findPaginated($query)
|
||||||
|
{
|
||||||
|
$queryObject = Elastica_Query::create($query);
|
||||||
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject);
|
||||||
|
$paginator = new Paginator($paginatorAdapter);
|
||||||
|
|
||||||
|
return $this->createPaginator($queryObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a paginator adapter for this query
|
||||||
|
*
|
||||||
|
* @param Elastica_Query $query
|
||||||
|
* @return DoctrinePaginatorAdapter
|
||||||
|
*/
|
||||||
|
protected function createPaginatorAdapter(Elastica_Query $query)
|
||||||
|
{
|
||||||
|
return new DoctrinePaginatorAdapter($this->searchable, $query, $this->mapper);
|
||||||
|
}
|
||||||
|
}
|
16
Finder/PaginatedFinderInterface.php
Normal file
16
Finder/PaginatedFinderInterface.php
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FOQ\ElasticaBundle\Finder;
|
||||||
|
|
||||||
|
use Zend\Paginator\Paginator;
|
||||||
|
|
||||||
|
interface PaginatedFinderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Searches for query results and returns them wrapped in a paginator
|
||||||
|
*
|
||||||
|
* @param mixed $query Can be a string, an array or an Elastica_Query object
|
||||||
|
* @return Paginator paginated results
|
||||||
|
*/
|
||||||
|
function findPaginated($query);
|
||||||
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
namespace FOQ\ElasticaBundle\Mapper;
|
namespace FOQ\ElasticaBundle\Mapper;
|
||||||
|
|
||||||
use FOQ\ElasticaBundle\MapperInterface;
|
use FOQ\ElasticaBundle\MapperInterface;
|
||||||
use Doctrine\Common\Persistence\ObjectRepository;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps Elastica documents with Doctrine objects
|
* Maps Elastica documents with Doctrine objects
|
||||||
|
@ -15,16 +15,16 @@ class DoctrineMapper implements MapperInterface
|
||||||
/**
|
/**
|
||||||
* Repository to fetch the objects from
|
* Repository to fetch the objects from
|
||||||
*
|
*
|
||||||
* @var ObjectRepository
|
* @var ObjectManager
|
||||||
*/
|
*/
|
||||||
protected $objectRepository = null;
|
protected $objectManager = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Doctrine identifier field
|
* Class of the model to map to the elastica documents
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $identifier = null;
|
protected $objectClass = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional parameters
|
* Optional parameters
|
||||||
|
@ -32,20 +32,21 @@ class DoctrineMapper implements MapperInterface
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $options = array(
|
protected $options = array(
|
||||||
'hydrate' => true
|
'hydrate' => true,
|
||||||
|
'identifier' => 'id'
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instanciates a new Mapper
|
* Instanciates a new Mapper
|
||||||
*
|
*
|
||||||
* @param ObjectRepository objectRepository
|
* @param ObjectManager objectManager
|
||||||
* @param string $identifier
|
* @param string $objectClass
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*/
|
*/
|
||||||
public function __construct(ObjectRepository $objectRepository, $identifier = 'id', array $options = array())
|
public function __construct(ObjectManager $objectManager, $objectClass, array $options = array())
|
||||||
{
|
{
|
||||||
$this->objectRepository = $objectRepository;
|
$this->objectManager = $objectManager;
|
||||||
$this->identifier = $identifier;
|
$this->objectClass = $objectClass;
|
||||||
$this->options = array_merge($this->options, $options);
|
$this->options = array_merge($this->options, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,13 +62,12 @@ class DoctrineMapper implements MapperInterface
|
||||||
return $elasticaObject->getId();
|
return $elasticaObject->getId();
|
||||||
}, $elasticaObjects);
|
}, $elasticaObjects);
|
||||||
|
|
||||||
return $this->objectRepository
|
return $this->objectManager
|
||||||
->createQueryBuilder()
|
->createQueryBuilder($this->objectClass)
|
||||||
->field($this->identifier)->in($ids)
|
->field($this->options['identifier'])->in($ids)
|
||||||
->hydrate($this->options['hydrate'])
|
->hydrate($this->options['hydrate'])
|
||||||
->getQuery()
|
->getQuery()
|
||||||
->execute()
|
->execute()
|
||||||
->toArray();
|
->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace FOQ\ElasticaBundle;
|
namespace FOQ\ElasticaBundle;
|
||||||
|
|
||||||
|
use FOQ\ElasticaBundle\Provider\ProviderInterface;
|
||||||
use Closure;
|
use Closure;
|
||||||
|
|
||||||
class Populator
|
class Populator
|
||||||
|
@ -13,6 +14,11 @@ class Populator
|
||||||
$this->providers = $providers;
|
$this->providers = $providers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addProvider($name, ProviderInterface $provider)
|
||||||
|
{
|
||||||
|
$this->providers[$name] = $provider;
|
||||||
|
}
|
||||||
|
|
||||||
public function populate(Closure $loggerClosure)
|
public function populate(Closure $loggerClosure)
|
||||||
{
|
{
|
||||||
foreach ($this->providers as $name => $provider) {
|
foreach ($this->providers as $name => $provider) {
|
||||||
|
|
|
@ -18,7 +18,8 @@ class DoctrineProvider implements ProviderInterface
|
||||||
protected $options = array(
|
protected $options = array(
|
||||||
'batch_size' => 100,
|
'batch_size' => 100,
|
||||||
'clear_object_manager' => true,
|
'clear_object_manager' => true,
|
||||||
'create_query_builder_method' => 'createQueryBuilder'
|
'query_builder_method' => 'createQueryBuilder',
|
||||||
|
'identifier' => 'id'
|
||||||
);
|
);
|
||||||
|
|
||||||
public function __construct(Elastica_Type $type, ObjectManager $objectManager, ObjectToArrayTransformerInterface $transformer, $objectClass, array $options = array())
|
public function __construct(Elastica_Type $type, ObjectManager $objectManager, ObjectToArrayTransformerInterface $transformer, $objectClass, array $options = array())
|
||||||
|
@ -40,16 +41,18 @@ class DoctrineProvider implements ProviderInterface
|
||||||
$queryBuilder = $this->createQueryBuilder();
|
$queryBuilder = $this->createQueryBuilder();
|
||||||
$nbObjects = $queryBuilder->getQuery()->count();
|
$nbObjects = $queryBuilder->getQuery()->count();
|
||||||
$fields = $this->extractTypeFields();
|
$fields = $this->extractTypeFields();
|
||||||
|
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||||
|
|
||||||
for ($offset = 0; $offset < $nbObjects; $offset += $this->options['batch_size']) {
|
for ($offset = 0; $offset < $nbObjects; $offset += $this->options['batch_size']) {
|
||||||
|
|
||||||
$loggerClosure(sprintf('%0.1f%% (%d/%d)', 100*$offset/$nbObjects, $offset, $nbObjects));
|
|
||||||
|
|
||||||
$objects = $queryBuilder->limit($this->options['batch_size'])->skip($offset)->getQuery()->execute()->toArray();
|
|
||||||
$documents = array();
|
$documents = array();
|
||||||
|
$objects = $queryBuilder->limit($this->options['batch_size'])->skip($offset)->getQuery()->execute()->toArray();
|
||||||
|
$stepCount = (count($objects)+$offset);
|
||||||
|
$loggerClosure(sprintf('%0.1f%% (%d/%d)', 100*$stepCount/$nbObjects, $stepCount, $nbObjects));
|
||||||
|
|
||||||
foreach ($objects as $object) {
|
foreach ($objects as $object) {
|
||||||
$data = $this->transformer->transform($object, $fields);
|
$data = $this->transformer->transform($object, $fields);
|
||||||
$documents[] = new Elastica_Document($object->getId(), $data);
|
$documents[] = new Elastica_Document($object->$identifierGetter(), $data);
|
||||||
}
|
}
|
||||||
$this->type->addDocuments($documents);
|
$this->type->addDocuments($documents);
|
||||||
|
|
||||||
|
@ -66,7 +69,7 @@ class DoctrineProvider implements ProviderInterface
|
||||||
**/
|
**/
|
||||||
protected function createQueryBuilder()
|
protected function createQueryBuilder()
|
||||||
{
|
{
|
||||||
return $this->objectManager->getRepository($this->objectClass)->{$this->options['create_query_builder_method']}();
|
return $this->objectManager->getRepository($this->objectClass)->{$this->options['query_builder_method']}();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function extractTypeFields()
|
protected function extractTypeFields()
|
||||||
|
@ -77,6 +80,9 @@ class DoctrineProvider implements ProviderInterface
|
||||||
// skip type name
|
// skip type name
|
||||||
$mappings = reset($mappings);
|
$mappings = reset($mappings);
|
||||||
$mappings = $mappings['properties'];
|
$mappings = $mappings['properties'];
|
||||||
|
if (array_key_exists('__isInitialized__', $mappings)) {
|
||||||
|
unset($mappings['__isInitialized__']);
|
||||||
|
}
|
||||||
|
|
||||||
return array_keys($mappings);
|
return array_keys($mappings);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,20 +12,31 @@
|
||||||
|
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
|
|
||||||
<service id="foq_elastica.index_manager" class="FOQ\ElasticaBundle\IndexManager">
|
<service id="foq_elastica.index_manager" class="FOQ\ElasticaBundle\IndexManager">
|
||||||
<argument /> <!-- indexes -->
|
<argument /> <!-- indexes -->
|
||||||
<argument /> <!-- default index -->
|
<argument /> <!-- default index -->
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="foq_elastica.populator" class="FOQ\ElasticaBundle\Populator">
|
<service id="foq_elastica.populator" class="FOQ\ElasticaBundle\Populator">
|
||||||
<argument /> <!-- providers -->
|
<argument /> <!-- providers -->
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="foq_elastica.reseter" class="FOQ\ElasticaBundle\Reseter">
|
<service id="foq_elastica.reseter" class="FOQ\ElasticaBundle\Reseter">
|
||||||
<argument type="service" id="foq_elastica.index_manager" />
|
<argument type="service" id="foq_elastica.index_manager" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="foq_elastica.mapping_setter" class="FOQ\ElasticaBundle\MappingSetter">
|
<service id="foq_elastica.mapping_setter" class="FOQ\ElasticaBundle\MappingSetter">
|
||||||
<argument /> <!-- mappings -->
|
<argument /> <!-- mappings -->
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="foq_elastica.transformer.auto" class="FOQ\ElasticaBundle\Transformer\ObjectToArrayAutomaticTransformer" public="false" />
|
<service id="foq_elastica.transformer.auto" class="FOQ\ElasticaBundle\Transformer\ObjectToArrayAutomaticTransformer" public="false" />
|
||||||
|
|
||||||
|
<service id="foq_elastica.finder.prototype" class="FOQ\ElasticaBundle\Finder\MappedFinder" public="true" abstract="true">
|
||||||
|
<argument /> <!-- index -->
|
||||||
|
<argument /> <!-- mapper -->
|
||||||
|
</service>
|
||||||
|
|
||||||
</services>
|
</services>
|
||||||
|
|
||||||
</container>
|
</container>
|
||||||
|
|
25
Resources/config/mongodb.xml
Normal file
25
Resources/config/mongodb.xml
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
|
||||||
|
<container xmlns="http://symfony.com/schema/dic/services"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||||
|
|
||||||
|
<services>
|
||||||
|
|
||||||
|
<service id="foq_elastica.provider.prototype.mongodb" class="FOQ\ElasticaBundle\Provider\DoctrineProvider" public="false" abstract="true">
|
||||||
|
<argument /> <!-- index -->
|
||||||
|
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
|
||||||
|
<argument type="service" id="foq_elastica.transformer.auto" />
|
||||||
|
<argument /> <!-- model -->
|
||||||
|
<argument type="collection" /> <!-- options -->
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<service id="foq_elastica.mapper.prototype.mongodb" class="FOQ\ElasticaBundle\Mapper\DoctrineMapper" public="false">
|
||||||
|
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
|
||||||
|
<argument /> <!-- model -->
|
||||||
|
<argument type="collection" /> <!-- options -->
|
||||||
|
</service>
|
||||||
|
|
||||||
|
</services>
|
||||||
|
|
||||||
|
</container>
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace FOQ\ElasticaBundle\Transformer;
|
namespace FOQ\ElasticaBundle\Transformer;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AutomaticObjectToArrayTransformer
|
* AutomaticObjectToArrayTransformer
|
||||||
* Tries to convert objects by generating getters
|
* Tries to convert objects by generating getters
|
||||||
|
@ -23,7 +25,7 @@ class ObjectToArrayAutomaticTransformer implements ObjectToArrayTransformerInter
|
||||||
foreach ($requiredKeys as $key) {
|
foreach ($requiredKeys as $key) {
|
||||||
$getter = 'get'.ucfirst($key);
|
$getter = 'get'.ucfirst($key);
|
||||||
if (!method_exists($class, $getter)) {
|
if (!method_exists($class, $getter)) {
|
||||||
throw new InvalidArgumentException(sprintf('The getter %s::%s does not exist', $this->objectClass, $getter));
|
throw new RuntimeException(sprintf('The getter %s::%s does not exist', $class, $getter));
|
||||||
}
|
}
|
||||||
$array[$key] = $object->$getter();
|
$array[$key] = $object->$getter();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue