Author: francois
Date: 2010-04-22 12:22:01 +0200 (Thu, 22 Apr 2010)
New Revision: 29233

Modified:
   plugins/sfPropel15Plugin/trunk/README
   
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelChoice.class.php
   
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelUnique.class.php
   plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPropelChoice.class.php
Log:
[sfPropelPlugin] Updated Propel validators to use the new Query objects

Modified: plugins/sfPropel15Plugin/trunk/README
===================================================================
--- plugins/sfPropel15Plugin/trunk/README       2010-04-22 09:19:49 UTC (rev 
29232)
+++ plugins/sfPropel15Plugin/trunk/README       2010-04-22 10:22:01 UTC (rev 
29233)
@@ -144,3 +144,17 @@
         ));
       }
     }
+
+### Updated Propel validators
+
+Both the `sfValidatorPropelChoice` and the `sfValidatorPropelUnique` were 
updated to use the new PropelQuery objects, and to accept a `query_methods` 
option similat to the one of `sfWidgetFormPropelChoice`. So if you display a 
selection of items using a query method, you can validate this selection, too:
+
+    [php]
+    class ContentForm extends BaseContentForm
+    {
+      public function configure()
+      {
+        $this->widgetSchema['section']->setOption('query_methods', 
array('published'));
+        $this->validatorSchema['section']->setOption('query_methods', 
array('published'));
+      }
+    }
\ No newline at end of file

Modified: 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelChoice.class.php
===================================================================
--- 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelChoice.class.php  
    2010-04-22 09:19:49 UTC (rev 29232)
+++ 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelChoice.class.php  
    2010-04-22 10:22:01 UTC (rev 29233)
@@ -24,6 +24,8 @@
    * Available options:
    *
    *  * model:      The model class (required)
+   *  * query_methods: An array of method names listing the methods to execute
+   *                on the model's query object
    *  * criteria:   A criteria to use when retrieving objects
    *  * column:     The column name (null by default which means we use the 
primary key)
    *                must be in field name format
@@ -37,6 +39,7 @@
   protected function configure($options = array(), $messages = array())
   {
     $this->addRequiredOption('model');
+    $this->addOption('query_methods', array());
     $this->addOption('criteria', null);
     $this->addOption('column', null);
     $this->addOption('connection', null);
@@ -53,7 +56,15 @@
    */
   protected function doClean($value)
   {
-    $criteria = null === $this->getOption('criteria') ? new Criteria() : clone 
$this->getOption('criteria');
+    $criteria = PropelQuery::from($this->getOption('model'));
+    if ($this->getOption('criteria'))
+    {
+      $criteria->mergeWith($this->getOption('criteria'));
+    }
+    foreach ($this->getOption('query_methods') as $method)
+    {
+      $criteria->$method();
+    }
 
     if ($this->getOption('multiple'))
     {
@@ -76,7 +87,7 @@
 
       $criteria->addAnd($this->getColumn(), $value, Criteria::IN);
 
-      $dbcount = 
call_user_func(array(constant($this->getOption('model').'::PEER'), 'doCount'), 
$criteria, $this->getOption('connection'));
+      $dbcount = $criteria->count($this->getOption('connection'));
 
       if ($dbcount != $count)
       {
@@ -87,7 +98,7 @@
     {
       $criteria->addAnd($this->getColumn(), $value);
 
-      $dbcount = 
call_user_func(array(constant($this->getOption('model').'::PEER'), 'doCount'), 
$criteria, $this->getOption('connection'));
+      $dbcount = $criteria->count($this->getOption('connection'));
 
       if (0 === $dbcount)
       {

Modified: 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelUnique.class.php
===================================================================
--- 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelUnique.class.php  
    2010-04-22 09:19:49 UTC (rev 29232)
+++ 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorPropelUnique.class.php  
    2010-04-22 10:22:01 UTC (rev 29233)
@@ -10,6 +10,7 @@
 
 /**
  * sfValidatorPropelUnique validates that the uniqueness of a column.
+ * This validator can only be used as a post validator.
  *
  * Warning: sfValidatorPropelUnique is susceptible to race conditions.
  * To avoid this issue, wrap the validation process and the model saving
@@ -43,6 +44,8 @@
    *  * model:              The model class (required)
    *  * column:             The unique column name in Propel field name format 
(required)
    *                        If the uniquess is for several columns, you can 
pass an array of field names
+   *  * query_methods:      An array of method names listing the methods to 
execute
+   *                        on the model's query object
    *  * field               Field name used by the form, other than the column 
name
    *  * primary_key:        The primary key column name in Propel field name 
format (optional, will be introspected if not provided)
    *                        You can also pass an array if the table has 
several primary keys
@@ -55,6 +58,7 @@
   {
     $this->addRequiredOption('model');
     $this->addRequiredOption('column');
+    $this->addOption('query_methods', array());
     $this->addOption('field', null);
     $this->addOption('primary_key', null);
     $this->addOption('connection', null);
@@ -84,13 +88,17 @@
     }
     $fields = $this->getOption('field');
 
-    $criteria = new Criteria();
+    $criteria = PropelQuery::from($this->getOption('model'));
+    foreach ($this->getOption('query_methods') as $method)
+    {
+      $criteria->$method();
+    }
     foreach ($this->getOption('column') as $i => $column)
     {
       $name = isset($fields[$i]) ? $fields[$i] : $column;
       if (!array_key_exists($name, $values))
       {
-        // one of the column has be removed from the form
+        // one of the column has been removed from the form
         return $values;
       }
 
@@ -99,7 +107,7 @@
       $criteria->add($colName, $values[$name]);
     }
 
-    $object = 
call_user_func(array(constant($this->getOption('model').'::PEER'), 
'doSelectOne'), $criteria, $this->getOption('connection'));
+    $object = $criteria->findOne($this->getOption('connection'));
 
     // if no object or if we're updating the object, it's ok
     if (null === $object || $this->isUpdate($object, $values))
@@ -154,13 +162,8 @@
     {
       $primaryKeys = array();
       $tableMap = 
call_user_func(array(constant($this->getOption('model').'::PEER'), 
'getTableMap'));
-      foreach ($tableMap->getColumns() as $column)
+      foreach ($tableMap->getPrimaryKeys() as $column)
       {
-        if (!$column->isPrimaryKey())
-        {
-          continue;
-        }
-
         $primaryKeys[] = 
call_user_func(array(constant($this->getOption('model').'::PEER'), 
'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, 
BasePeer::TYPE_FIELDNAME);
       }
 

Modified: 
plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPropelChoice.class.php
===================================================================
--- 
plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPropelChoice.class.php    
    2010-04-22 09:19:49 UTC (rev 29232)
+++ 
plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPropelChoice.class.php    
    2010-04-22 10:22:01 UTC (rev 29233)
@@ -41,10 +41,12 @@
    *  * order_by:    An array composed of two fields:
    *                   * The column to order by the results (must be in the 
PhpName format)
    *                   * asc or desc
+   *  * query_methods: An array of method names listing the methods to execute
+   *                 on the model's query object
    *  * criteria:    A criteria to use when retrieving objects
    *  * connection:  The Propel connection to use (null by default)
    *  * multiple:    true if the select tag must allow multiple selections
-   *  * peer_method: The peer method to use to fetch objects
+   *  * peer_method: ignored - only supported for BC purpose
    *
    * @see sfWidgetFormSelect
    */
@@ -78,8 +80,6 @@
       $choices[''] = true === $this->getOption('add_empty') ? '' : 
$this->getOption('add_empty');
     }
 
-    $class = constant($this->getOption('model').'::PEER');
-
     $criteria = PropelQuery::from($this->getOption('model'));
     if ($this->getOption('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