Changeset 5033
- Timestamp:
- 10/02/08 07:07:08 (9 months ago)
- Location:
- branches/1.1
- Files:
-
- 1 added
- 6 modified
-
lib/Doctrine/Record/Abstract.php (modified) (1 diff)
-
lib/Doctrine/Table.php (modified) (4 diffs)
-
lib/Doctrine/Validator.php (modified) (1 diff)
-
lib/Doctrine/Validator/Unique.php (modified) (2 diffs)
-
tests/run.php (modified) (1 diff)
-
tests/Ticket/255TestCase.php (added)
-
UPGRADE_TO_1_1 (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.1/lib/Doctrine/Record/Abstract.php
r5030 r5033 113 113 } 114 114 } 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 115 136 public function setAttribute($attr, $value) 116 137 { -
branches/1.1/lib/Doctrine/Table.php
r5030 r5033 84 84 85 85 /** 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 /** 86 93 * @var array $_fieldNames an array of field names. used to look up field names 87 94 * from column names. … … 807 814 808 815 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; 809 832 } 810 833 … … 1753 1776 1754 1777 /** 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 /** 1755 1804 * getColumnCount 1756 1805 * … … 1815 1864 { 1816 1865 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; 1817 1876 } 1818 1877 -
branches/1.1/lib/Doctrine/Validator.php
r4998 r5033 77 77 $table->validateField($fieldName, $value, $record); 78 78 } 79 $table->validateUniques($record); 79 80 } 80 81 -
branches/1.1/lib/Doctrine/Validator/Unique.php
r3884 r5033 48 48 } 49 49 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 } 54 59 55 60 // If the record is not new we need to add primary key checks because its ok if the … … 63 68 } 64 69 } 65 70 66 71 $stmt = $table->getConnection()->getDbh()->prepare($sql); 67 72 $stmt->execute($values); -
branches/1.1/tests/run.php
r5032 r5033 28 28 $tickets->addTestCase(new Doctrine_Ticket_Njero_TestCase()); 29 29 $tickets->addTestCase(new Doctrine_Ticket_Ayoub_TestCase()); 30 $tickets->addTestCase(new Doctrine_Ticket_255_TestCase()); 30 31 $tickets->addTestCase(new Doctrine_Ticket_381_TestCase()); 31 32 $tickets->addTestCase(new Doctrine_Ticket_384_TestCase()); -
branches/1.1/UPGRADE_TO_1_1
r5032 r5033 93 93 ------------------- 94 94 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. 96 96 97 97 $q = Doctrine_Query::create() … … 114 114 ) 115 115 */ 116 117 Doctrine 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 }