Skip to content

Commit

Permalink
Merge pull request #8807 from cakephp/issue-8803
Browse files Browse the repository at this point in the history
Fix IsUnique rule & null values
  • Loading branch information
markstory committed May 11, 2016
2 parents ddbbd93 + 02c8f32 commit f61575f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/Rule/IsUnique.php
Expand Up @@ -77,7 +77,7 @@ protected function _alias($alias, $conditions)
{
$aliased = [];
foreach ($conditions as $key => $value) {
$aliased["$alias.$key"] = $value;
$aliased["$alias.$key IS"] = $value;
}
return $aliased;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Expand Up @@ -369,6 +369,33 @@ public function testIsUniqueMultipleFields()
$this->assertSame($entity, $table->save($entity));
}

/**
* Tests isUnique with multiple fields and a nulled field.
*
* @group save
* @return void
*/
public function testIsUniqueMultipleFieldsOneIsNull()
{
$entity = new Entity([
'author_id' => null,
'title' => 'First Article'
]);
$table = TableRegistry::get('Articles');
$rules = $table->rulesChecker();
$rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));

$this->assertSame($entity, $table->save($entity));

// Make a duplicate
$entity = new Entity([
'author_id' => null,
'title' => 'First Article'
]);
$this->assertFalse($table->save($entity));
$this->assertEquals(['title' => ['_isUnique' => 'Nope']], $entity->errors());
}

/**
* Tests the existsIn domain rule
*
Expand Down

0 comments on commit f61575f

Please sign in to comment.