Author: francois
Date: 2010-03-23 12:29:36 +0100 (Tue, 23 Mar 2010)
New Revision: 28699

Modified:
   plugins/sfPropel15Plugin/trunk/README
   plugins/sfPropel15Plugin/trunk/lib/form/sfFormFilterPropel.class.php
Log:
[sfPropel15Plugin] Added an easy way to add a custom filter in the admin 
generator list view

Modified: plugins/sfPropel15Plugin/trunk/README
===================================================================
--- plugins/sfPropel15Plugin/trunk/README       2010-03-23 11:00:05 UTC (rev 
28698)
+++ plugins/sfPropel15Plugin/trunk/README       2010-03-23 11:29:36 UTC (rev 
28699)
@@ -218,4 +218,45 @@
       fields:
         Author: { link_module: author }
         
-You no longer need a partial for such simple cases. This should unclutter the 
`templates/` directory of your admin generator modules.
\ No newline at end of file
+You no longer need a partial for such simple cases. This should unclutter the 
`templates/` directory of your admin generator modules.
+
+### Easy Custom Filter ###
+
+Adding custom filters becomes very easy once you can take advantage of the 
generated Propel query classes. For example, in a list of `Books`, the default 
filters offer one text input for the book title, and a second one for the book 
ISBN. In order to replace them with a single text input, here is what you need 
to do:
+
+    [php]    
+    // in lib/filter/BookFormFilter.php
+    class BookFormFilter extends BaseBookFormFilter
+    {
+      public function configure()
+      {
+        $this->widgetSchema['full_text'] = new 
sfWidgetFormFilterInput(array('with_empty' => false));
+        $this->validatorSchema['full_text'] = new 
sfValidatorPass(array('required' => false));
+      }
+    }
+    
+    // in lib/model/Bookquery.php
+    class BookQuery extends BaseBookQuery
+    {
+      public function filterByFullText($value)
+      {
+        if (isset($value['text'])) {
+        $pattern = '%' . $value['text'] . '%';
+        $this
+          ->condition('condTitle', 'Book.Title LIKE ?', $pattern)
+          ->condition('condISBN', 'Book.ISBN LIKE ?', $pattern)
+          ->combine(array('condTitle', 'condISBN'), Criteria::LOGICAL_OR);
+        }
+        return $this;
+      }
+    }
+
+Now just modify the `filters.display` setting in the `generator.yml` to remove 
the `title` and `isbn` filters, and replace them with the new `full_text` 
filter:
+
+    [yaml]
+    # in modules/book/config/generator.yml
+    config:
+      filters:
+        display: [full_text]
+
+The amdin generator looks for a `filterByXXX()` method in the query class, 
where `XXX` is the CamelCase version of the custom filter you add.
\ No newline at end of file

Modified: plugins/sfPropel15Plugin/trunk/lib/form/sfFormFilterPropel.class.php
===================================================================
--- plugins/sfPropel15Plugin/trunk/lib/form/sfFormFilterPropel.class.php        
2010-03-23 11:00:05 UTC (rev 28698)
+++ plugins/sfPropel15Plugin/trunk/lib/form/sfFormFilterPropel.class.php        
2010-03-23 11:29:36 UTC (rev 28699)
@@ -135,30 +135,34 @@
 
       try
       {
-        $method = sprintf('add%sColumnCriteria', call_user_func(array($peer, 
'translateFieldName'), $field, BasePeer::TYPE_FIELDNAME, 
BasePeer::TYPE_PHPNAME));
+        $ucField = call_user_func(array($peer, 'translateFieldName'), $field, 
BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
+        $isReal = true;
       }
       catch (Exception $e)
       {
-        // not a "real" column
-        if (!method_exists($this, $method = sprintf('add%sColumnCriteria', 
self::camelize($field))))
-        {
-          throw new LogicException(sprintf('You must define a "%s" method to 
be able to filter with the "%s" field.', $method, $field));
-        }
+        $ucField = self::camelize($field);
+        $isReal = false;
       }
-
-      if (method_exists($this, $method))
+      
+      if (method_exists($this, $method = sprintf('add%sColumnCriteria', 
$ucField)))
       {
+        // FormFilter::add[ColumnName]Criteria
         $this->$method($criteria, $field, $values[$field]);
       }
-      else
+      elseif ($isReal && method_exists($this, $method = 
sprintf('add%sCriteria', $type)))
       {
-        if (!method_exists($this, $method = sprintf('add%sCriteria', $type)))
-        {
-          throw new LogicException(sprintf('Unable to filter for the "%s" 
type.', $type));
-        }
-
+        // FormFilter::add[ColumnType]Criteria
         $this->$method($criteria, $field, $values[$field]);
       }
+      elseif (method_exists($criteria, $method = sprintf('filterBy%s', 
$ucField)))
+      {
+        // ModelCriteria::filterBy[ColumnName]
+        $criteria->$method($values[$field]);
+      }
+      else
+      {
+        throw new LogicException(sprintf('You must define a "%s" method in the 
%s class to be able to filter with the "%s" field.', sprintf('filterBy%s', 
$ucField), get_class($criteria), $field));
+      }
     }
 
     return $criteria;

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to