Changeset 5033

Show
Ignore:
Timestamp:
10/02/08 07:07:08 (9 months ago)
Author:
jwage
Message:

[1.1] fixes #255 Added ability to validate a group of fields

Location:
branches/1.1
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • branches/1.1/lib/Doctrine/Record/Abstract.php

    r5030 r5033  
    113113        } 
    114114    } 
     115 
     116    /** 
     117     * Specify an array of fields that are unique and will be validated as such 
     118     * 
     119     * @return void 
     120     */ 
     121    public function unique() 
     122    { 
     123        $args = func_get_args(); 
     124 
     125        if (count($args) == 1) { 
     126            $fields = (array) $args[0]; 
     127        } else if (count($args) > 1) { 
     128            $fields = $args; 
     129        } else { 
     130            throw new Doctrine_Record_Exception('You must specify the fields to make a unique constraint on.'); 
     131        } 
     132 
     133        return $this->_table->unique($fields); 
     134    } 
     135 
    115136    public function setAttribute($attr, $value) 
    116137    { 
  • branches/1.1/lib/Doctrine/Table.php

    r5030 r5033  
    8484 
    8585    /** 
     86     * Array of unique sets of fields. These values are validated on save 
     87     * 
     88     * @var array $_uniques 
     89     */ 
     90    protected $_uniques = array(); 
     91 
     92    /** 
    8693     * @var array $_fieldNames            an array of field names. used to look up field names 
    8794     *                                    from column names. 
     
    807814 
    808815        return false; 
     816    } 
     817 
     818    /** 
     819     * Add set of fields to be unique. Will automatically create unique 
     820     * index and validate the values on save 
     821     * 
     822     * @param array $fields  
     823     * @return void 
     824     */ 
     825    public function unique($fields) 
     826    { 
     827        $name = implode('_', $fields) . '_unqidx'; 
     828        $definition = array('type' => 'unique', 'fields' => $fields); 
     829        $this->addIndex($name, $definition); 
     830 
     831        $this->_uniques[] = $fields; 
    809832    } 
    810833 
     
    17531776 
    17541777    /** 
     1778     * Validate all the unique sets of fields for the given Doctrine_Record instance 
     1779     * 
     1780     * @param Doctrine_Record $record 
     1781     */ 
     1782    public function validateUniques(Doctrine_Record $record) 
     1783    { 
     1784        $errorStack = $record->getErrorStack(); 
     1785        $validator = Doctrine_Validator::getValidator('unique'); 
     1786        $validator->invoker = $record; 
     1787 
     1788        foreach ($this->_uniques as $fields) 
     1789        { 
     1790            $validator->field = $fields; 
     1791            $values = array(); 
     1792            foreach ($fields as $field) { 
     1793                $values[] = $record->$field; 
     1794            } 
     1795            if ( ! $validator->validate($values)) { 
     1796                foreach ($fields as $field) { 
     1797                    $errorStack->add($field, $validator); 
     1798                } 
     1799            } 
     1800        } 
     1801    } 
     1802 
     1803    /** 
    17551804     * getColumnCount 
    17561805     * 
     
    18151864    { 
    18161865        return $this->getColumnNames((array) $this->getIdentifier()); 
     1866    } 
     1867 
     1868    /** 
     1869     * Get array of sets of unique fields 
     1870     * 
     1871     * @return array $uniques 
     1872     */ 
     1873    public function getUniques() 
     1874    { 
     1875        return $this->_uniques; 
    18171876    } 
    18181877 
  • branches/1.1/lib/Doctrine/Validator.php

    r4998 r5033  
    7777            $table->validateField($fieldName, $value, $record); 
    7878        } 
     79        $table->validateUniques($record); 
    7980    } 
    8081 
  • branches/1.1/lib/Doctrine/Validator/Unique.php

    r3884 r5033  
    4848        } 
    4949 
    50         $sql   = 'SELECT ' . $pks . ' FROM ' . $table->getTableName() . ' WHERE ' . $this->field . ' = ?'; 
    51          
    52         $values = array(); 
    53         $values[] = $value; 
     50        $sql   = 'SELECT ' . $pks . ' FROM ' . $table->getTableName(); 
     51        if (is_array($this->field)) { 
     52            $sql .= ' WHERE ' . implode(' = ? AND ', $this->field) . ' = ?'; 
     53            $values = $value; 
     54        } else { 
     55            $sql .= ' WHERE ' . $this->field . ' = ?'; 
     56            $values = array(); 
     57            $values[] = $value; 
     58        } 
    5459         
    5560        // If the record is not new we need to add primary key checks because its ok if the  
     
    6368            } 
    6469        } 
    65          
     70 
    6671        $stmt  = $table->getConnection()->getDbh()->prepare($sql); 
    6772        $stmt->execute($values); 
  • branches/1.1/tests/run.php

    r5032 r5033  
    2828$tickets->addTestCase(new Doctrine_Ticket_Njero_TestCase()); 
    2929$tickets->addTestCase(new Doctrine_Ticket_Ayoub_TestCase()); 
     30$tickets->addTestCase(new Doctrine_Ticket_255_TestCase()); 
    3031$tickets->addTestCase(new Doctrine_Ticket_381_TestCase()); 
    3132$tickets->addTestCase(new Doctrine_Ticket_384_TestCase()); 
  • branches/1.1/UPGRADE_TO_1_1

    r5032 r5033  
    9393------------------- 
    9494 
    95 You can now convert a Doctrine_Collection in to a key value array made up of the values from the two specified columns. 
     95[r5032](http://trac.doctrine-project.org/changeset/5032) - You can now convert a Doctrine_Collection in to a key value array made up of the values from the two specified columns. 
    9696 
    9797    $q = Doctrine_Query::create() 
     
    114114    ) 
    115115    */ 
     116 
     117Doctrine Validation 
     118------------------- 
     119 
     120[r5033](http://trac.doctrine-project.org/changeset/5033) - You can now specify a unique validator on a set of fields. The argument of the unique() function can either be an array of fields or an argument for each field. 
     121 
     122    public function setTableDefinition() 
     123    { 
     124        $this->hasColumn('username', 'string', 255); 
     125        $this->hasColumn('email_address', 'string', 255); 
     126 
     127        $this->unique('username', 'email_address'); 
     128    }