Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "filters"

Forward in filters

The regular use of forward is causing double content rendering and it took me a while to find the proper solution so here it is:

<?php
 
class SiteFilter extends sfFilter
{
 
  public function execute($filterChain)
  {
    $context = $this->getContext();
 
    if($this->isFirstCall()){
      if(true){ // put some condition in here
        $context->getController()->forward('module', 'action');
        throw new sfStopException(); // the solution
      }
    }
 
    $filterChain->execute();
  }
}
 

As you can see sfStopException() is the key.

by rozwell on 2012-06-08, tagged content  double  filter  filters  forward  rendering 

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)