Skip to content
Permalink
Browse files

Using bindingKey in existsIn, fixes #7709

  • Loading branch information...
lorenzo committed Dec 6, 2015
1 parent c2bd05c commit 3bb16bf797849ade0f8ad90a77e4f9e1ce7cdd94
Showing with 37 additions and 4 deletions.
  1. +10 −4 src/ORM/Rule/ExistsIn.php
  2. +27 −0 tests/TestCase/ORM/RulesCheckerIntegrationTest.php
@@ -66,10 +66,16 @@ public function __invoke(EntityInterface $entity, array $options)
}
$source = !empty($options['repository']) ? $options['repository'] : $this->_repository;
$source = $source instanceof Association ? $source->source() : $source;
$target = $this->_repository instanceof Association ?
$this->_repository->target() :
$this->_repository;
$target = $this->_repository;
if ($target instanceof Association) {
$bindingKey = (array)$target->bindingKey();
$target = $this->_repository->target();
} else {
$bindingKey = (array)$target->primaryKey();
}
if (!empty($options['_sourceTable']) && $target === $options['_sourceTable']) {
return true;
@@ -92,7 +98,7 @@ public function __invoke(EntityInterface $entity, array $options)
$primary = array_map(
[$this->_repository, 'aliasField'],
(array)$this->_repository->primaryKey()
$bindingKey
);
$conditions = array_combine(
$primary,
@@ -433,6 +433,33 @@ public function testExistsInNullValue()
$this->assertEquals([], $entity->errors('author_id'));
}
/**
* Tests exists in uses the bindingKey of the association
*
* @return
*/
public function testExistsInWithBindingKey()
{
$entity = new Entity([
'title' => 'An Article',
]);
$table = TableRegistry::get('Articles');
$table->belongsTo('Authors', [
'bindingKey' => 'name',
'foreignKey' => 'title'
]);
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('title', 'Authors'));
$this->assertFalse($table->save($entity));
$this->assertNotEmpty($entity->errors('title'));
$entity->clean();
$entity->title = 'larry';
$this->assertEquals($entity, $table->save($entity));
}
/**
* Tests the checkRules save option
*

0 comments on commit 3bb16bf

Please sign in to comment.
You can’t perform that action at this time.