Skip to content

Commit

Permalink
Fixes ambiguous column error in ExistsIn, fixes #5877
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 12, 2015
1 parent deca637 commit 0b65594
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/ORM/Rule/ExistsIn.php
Expand Up @@ -78,8 +78,15 @@ public function __invoke(EntityInterface $entity, array $options)
return true;
}

$alias = $this->_repository->alias();
$primary = array_map(
function ($key) use ($alias) {
return "$alias.$key";
},

This comment has been minimized.

Copy link
@markstory

markstory Feb 13, 2015

Member

Will the Unique rule have similar issues?

(array)$this->_repository->primaryKey()
);
$conditions = array_combine(
(array)$this->_repository->primaryKey(),
$primary,
$entity->extract($this->_fields)
);
return $this->_repository->exists($conditions);
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Expand Up @@ -548,6 +548,32 @@ public function testExistsInWithCleanFields()
$this->assertSame($entity, $table->save($entity));
}

/**
* Tests the existsIn with coflicting columns
*
* @group save
* @return void
*/
public function testExistsInAliasPrefix()
{
$entity = new Entity([
'title' => 'An Article',
'author_id' => 500
]);

$table = TableRegistry::get('Articles');
$table->belongsTo('Authors');
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('author_id', 'Authors'));

$table->Authors->eventManager()->on('Model.beforeFind', function ($event, $query) {
$query->leftJoin(['a2' => 'authors']);
});

$this->assertFalse($table->save($entity));
$this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
}

/**
* Tests using rules to prevent delete operations
*
Expand Down

0 comments on commit 0b65594

Please sign in to comment.