Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "caseinsensitive"

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)

Case-insensitive URLs/routes

This works for symfony 1.2 and might be backwards compatible with 1.1 and 1.0, but I haven't tested it with those versions.

Place this file in an appropriate directory (eg. apps/myapp/lib/routing):

sfRegexExtendedRoute.class.php:

<?php
 
class sfRegexExtendedRoute extends sfRoute
{
  protected function compile()
  {
    $return = parent::compile();
 
    // Case-insensitive matching (default symfony behaviour = NOT case-insensitive).
    if (isset($this->options['case_sensitive']) && !$this->options['case_sensitive'])
    {
      // Make sure $this->regex is in the form "#...#[???]"
      // and that [???] does not already include "i" (the case-insensitive flag).
      if (($parts = explode('#', strrev($this->regex)))
          && 3 <= ($num_parts = count($parts))
          && '' == $parts[$num_parts-1]
          && false === strpos($parts[0], 'i'))
      {
        $this->regex .= 'i';
      }
    }
 
    return $return;
  }
}
 

Add the option case_sensitive: false in the route you want to make case-insensitive and set its class to sfRegexExtendedRoute:

apps/myapp/config/routing.yml:

...
my_route:
  url:      /my/case/insensitive/url/:whatever
  options:  { case_sensitive: false }
  class:    sfRegexExtendedRoute
...
 

Clear your cache:

> php symfony cc
 

Your url matching for that route should now be case-insensitive.

Changing case_sensitive: false to case_sensitive: true in routing.yml will revert the matching for that route back to (symfony default) case-sensitive matching.

by Nickolas Daskalou on 2009-05-24, tagged caseinsensitive  casesensistive  expression  regex  regular  route  routes  routing