From 44d8236f21db7a7d5a89cd43d6a98180e03db306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 1 Dec 2013 22:53:37 +0100 Subject: [PATCH] add doublemetaphone function and config to enable-disable usage of double metaphone --- Controller/PersonController.php | 23 ++++++++++++++----- .../CLChillPersonExtension.php | 3 +++ DependencyInjection/Configuration.php | 12 ++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Controller/PersonController.php b/Controller/PersonController.php index 148fbfa77..5f003e9e4 100644 --- a/Controller/PersonController.php +++ b/Controller/PersonController.php @@ -101,14 +101,25 @@ class PersonController extends Controller { $offset = $this->getRequest()->query->getInt('offet', 0); $limit = $this->getRequest()->query->getInt('limit', 30); - $persons = $em->createQuery('SELECT p FROM CLChillPersonBundle:Person p' - . ' WHERE LOWER(p.name) like LOWER(:q) OR LOWER(p.surname) ' - . ' like LOWER(:q) ') - ->setParameter('q', '%'.$q.'%') + $dql = 'SELECT p FROM CLChillPersonBundle:Person p' + . ' WHERE' + . ' LOWER(p.name) like LOWER(:q)' + . ' OR LOWER(p.surname) like LOWER(:q)'; + + if ($this->container->getParameter('cl_chill_person.search.use_double_metaphone')) { + $dql .= ' OR DOUBLEMETAPHONE(p.name) = DOUBLEMETAPHONE(:qabsolute)'; + } + + + $query = $em->createQuery($dql) + ->setParameter('q', '%'.$q.'%'); + if ($this->container->getParameter('cl_chill_person.search.use_double_metaphone')) { + $query->setParameter('qabsolute', $q); + } + // ->setOffset($offset) // ->setLimit($limit) - ->getResult() - ; + $persons = $query->getResult() ; if (count($persons) === 0 ){ diff --git a/DependencyInjection/CLChillPersonExtension.php b/DependencyInjection/CLChillPersonExtension.php index 908c5751a..a21faa47f 100644 --- a/DependencyInjection/CLChillPersonExtension.php +++ b/DependencyInjection/CLChillPersonExtension.php @@ -21,6 +21,9 @@ class CLChillPersonExtension extends Extension { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); + + $container->setParameter('cl_chill_person.search.use_double_metaphone', + $config['search']['use_double_metaphone']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 01c04eb07..67776958b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -19,6 +19,18 @@ class Configuration implements ConfigurationInterface { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('cl_chill_person'); + + $rootNode + ->canBeDisabled() + ->children() + ->arrayNode('search') + ->canBeDisabled() + ->children() + ->booleanNode('use_double_metaphone') + ->defaultFalse() + ->end() + ->booleanNode('use_trigrams')->defaultFalse()->end(); + // Here you should define the parameters that are allowed to // configure your bundle. See the documentation linked above for