Improved the previous fix for the unique validator.
This commit is contained in:
parent
18766e6579
commit
635bc1fa66
@ -47,10 +47,16 @@ class Doctrine_Validator_Unique
|
|||||||
$values = array();
|
$values = array();
|
||||||
$values[] = $value;
|
$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) {
|
foreach ($table->getPrimaryKeys() as $pk) {
|
||||||
$sql .= " AND {$pk} != ?";
|
$sql .= " AND {$pk} != ?";
|
||||||
$values[] = $record->$pk;
|
$values[] = $record->$pk;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$stmt = $table->getConnection()->getDbh()->prepare($sql);
|
$stmt = $table->getConnection()->getDbh()->prepare($sql);
|
||||||
$stmt->execute($values);
|
$stmt->execute($values);
|
||||||
|
@ -333,7 +333,7 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase {
|
|||||||
*
|
*
|
||||||
* @todo move to a separate test file (tests/Validator/UniqueTestCase) .
|
* @todo move to a separate test file (tests/Validator/UniqueTestCase) .
|
||||||
*/
|
*/
|
||||||
public function testSetSameUniqueValueThrowsNoException()
|
public function testSetSameUniqueValueOnSameRecordThrowsNoException()
|
||||||
{
|
{
|
||||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
|
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
|
||||||
|
|
||||||
@ -346,11 +346,33 @@ class Doctrine_Validator_TestCase extends Doctrine_UnitTestCase {
|
|||||||
try {
|
try {
|
||||||
$r->save();
|
$r->save();
|
||||||
}
|
}
|
||||||
catch(Doctrine_Validator_Exception $e) {
|
catch (Doctrine_Validator_Exception $e) {
|
||||||
$this->fail("Validator exception raised without reason!");
|
$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);
|
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user