1
0
mirror of synced 2025-01-20 15:31:40 +03:00

Improved the previous fix for the unique validator.

This commit is contained in:
romanb 2007-04-12 13:37:45 +00:00
parent 18766e6579
commit 635bc1fa66
2 changed files with 36 additions and 8 deletions

View File

@ -47,10 +47,16 @@ class Doctrine_Validator_Unique
$values = array();
$values[] = $value;
// If the record is not new we need to add primary key checks because its ok if the
// unique value already exists in the database IF the record in the database is the same
// as the one that is validated here.
$state = $record->getState();
if (! ($state == Doctrine_Record::STATE_TDIRTY || $state == Doctrine_Record::STATE_TCLEAN)) {
foreach ($table->getPrimaryKeys() as $pk) {
$sql .= " AND {$pk} != ?";
$values[] = $record->$pk;
}
}
$stmt = $table->getConnection()->getDbh()->prepare($sql);
$stmt->execute($values);

View File

@ -333,7 +333,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase {
*
* @todo move to a separate test file (tests/Validator/UniqueTestCase) .
*/
public function testSetSameUniqueValueThrowsNoException()
public function testSetSameUniqueValueOnSameRecordThrowsNoException()
{
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
@ -348,9 +348,31 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase {
}
catch (Doctrine_Validator_Exception $e) {
$this->fail("Validator exception raised without reason!");
var_dump($r->getErrorStack());
}
$r->delete(); // clean up
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
}
public function testSetSameUniqueValueOnDifferentRecordThrowsException()
{
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
$r = new ValidatorTest_Person();
$r->identifier = '1234';
$r->save();
$r = new ValidatorTest_Person();
$r->identifier = 1234;
try {
$r->save();
$this->fail("No validator exception thrown on unique validation.");
} catch (Doctrine_Validator_Exception $e) {
$this->pass();
}
$r->delete(); // clean up
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
}
}