Author: Russ
Date: 2010-02-07 16:52:33 +0100 (Sun, 07 Feb 2010)
New Revision: 27641

Modified:
   
plugins/sfDoctrinePlugin/branches/1.3-2.0/lib/validator/sfValidatorDoctrineUnique.class.php
Log:
[1.4][sfDoctrine2plugin] Update unique validator for Doctrine 2.0


Modified: 
plugins/sfDoctrinePlugin/branches/1.3-2.0/lib/validator/sfValidatorDoctrineUnique.class.php
===================================================================
--- 
plugins/sfDoctrinePlugin/branches/1.3-2.0/lib/validator/sfValidatorDoctrineUnique.class.php
 2010-02-07 14:28:53 UTC (rev 27640)
+++ 
plugins/sfDoctrinePlugin/branches/1.3-2.0/lib/validator/sfValidatorDoctrineUnique.class.php
 2010-02-07 15:52:33 UTC (rev 27641)
@@ -20,15 +20,16 @@
  * @subpackage doctrine
  * @author     Fabien Potencier <[email protected]>
  * @author     Jonathan H. Wage <[email protected]>
+ * @author     Russell Flynn    <[email protected]>
  * @version    SVN: $Id: sfValidatorDoctrineUnique.class.php 8807 2008-05-06 
14:12:28Z fabien $
  */
 class sfValidatorDoctrineUnique extends sfValidatorSchema
 {
-       protected $em;
+  protected $em;
 
   public function __construct(\Doctrine\ORM\EntityManager $em, $options = 
array(), $messages = array())
   {
-               $this->em = $em;
+    $this->em = $em;
     parent::__construct(null, $options, $messages);
   }
 
@@ -64,7 +65,7 @@
   protected function doClean($values)
   {
     $originalValues = $values;
-    $table = Doctrine::getTable($this->getOption('model'));
+
     if (!is_array($this->getOption('column')))
     {
       $this->setOption('column', array($this->getOption('column')));
@@ -78,20 +79,21 @@
       $values = array($columns[0] => $values);
     }
 
-    $q = Doctrine::getTable($this->getOption('model'))->createQuery('a');
+    $qb = 
$this->em->createQueryBuilder()->select('a')->from($this->getOption('model'), 
'a');
+    $i  = 0;
     foreach ($this->getOption('column') as $column)
     {
-      $colName = $table->getColumnName($column);
       if (!array_key_exists($column, $values))
       {
-        // one of the column has be removed from the form
+        // one of the columns has be removed from the form
         return $originalValues;
       }
 
-      $q->addWhere('a.' . $colName . ' = ?', $values[$column]);
+      $qb->andWhere('a.' . $column . ' = ?'.++$i);
+      $qb->setParameter($i, $values[$column]);
     }
 
-    $object = $q->fetchOne();
+    $object = $qb->setMaxResults(1)->getQuery()->execute();
 
     // if no object or if we're updating the object, it's ok
     if (!$object || $this->isUpdate($object, $values))
@@ -114,12 +116,12 @@
   /**
    * Returns whether the object is being updated.
    *
-   * @param BaseObject  A Doctrine object
-   * @param array       An array of values
+   * @param object A compatable object
+   * @param array  An array of values
    *
-   * @param Boolean     true if the object is being updated, false otherwise
+   * @param Boolean true if the object is being updated, false otherwise
    */
-  protected function isUpdate(Doctrine_Record $object, $values)
+  protected function isUpdate($object, $values)
   {
     // check each primary key column
     foreach ($this->getPrimaryKeys() as $column)
@@ -142,8 +144,9 @@
   {
     if (null === $this->getOption('primary_key'))
     {
-      $primaryKeys = 
Doctrine::getTable($this->getOption('model'))->getIdentifier();
-      $this->setOption('primary_key', $primaryKeys);
+      $primaryKeyColumns = 
$this->em->getClassMetadata($this->getOption("model"))->getIdentifier();
+
+      $this->setOption('primary_key', $primaryKeyColumns);
     }
 
     if (!is_array($this->getOption('primary_key')))

-- 
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