Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "ilike"

Case-unsensitive filters with Symfony and Doctrine

Problematic

Environnement : Symfony 1.2 - sfDoctrinePlugin

Context : doctrine:generate-admin modules

Problème : filters case-sensitive

Objective : make filters case-unsensitive

Analysis

Every classes inherited from sfFormFilter alse inherite from BaseFormFilter, which is modifyable in every project. In the Doctrine case, sfFormFilterDoctrine and BaseFormFilterDoctrine. This class "Base" is here :

lib/filter/doctrine/BaseFormFilterDoctrine.class.php
 

The aimed method is the one which works on text fields. The line we want is the line 209 of the file symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php :

$query->addWhere('r.' . $fieldName . ' LIKE ?', '%' . $values['text'] . '%');
 

So a solution should be the override this method replacing LIKE by ILIKE. Let's try :

Solution

Overriding the class sfFormFilterDoctrine's method addTextQuery() should give you this kind of stuff if there is no more work done on it :

# lib/filter/doctrine/BaseFormFilterDoctrine.class.php
 /**
  * Project filter form base class.
  *
  * @packagefilters
  *
  * @versionSVN: $Id: sfDoctrineFormFilterBaseTemplate.php 11675 2008-09-19 15:21:38Z fabien $
  */
 abstract class BaseFormFilterDoctrine extends sfFormFilterDoctrine
 {
   public function setup()
   {
   }
 
   // totally inspirated from lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php
   // function addTextQuery()
   // line modified : 209
   // be careful on symfony updates to update this method if there are changes
   protected function addTextQuery(Doctrine_Query $query, $field, $values)
   {
     $fieldName = $this->getFieldName($field);
 
     if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
     {
       $query->addWhere('r.' . $fieldName . ' IS NULL');
     }
     else if (is_array($values) && isset($values['text']) &&  != $values['text'])
     {
       // Here is the change, with "ILIKE" instead of "LIKE"
       $query->addWhere('r.' . $fieldName . ' ILIKE ?', '%' . $values['text'] . '%');
     }
   }
 }
 

References

The original documentation in french is available here :

http://www.e-glop.net/main/Filtres_case-unsensitive_dans_Symfony

by Baptiste SIMON on 2009-06-15, tagged caseinsensitive  casesensitive  filters  ilike 
(1 comment)