Skip to content
Permalink
Browse files

Avoiding ambiguos column erros when saving, fixes #3803

  • Loading branch information...
lorenzo committed Jun 26, 2014
1 parent 5502418 commit 5b5d537de840f73322400589212471c6058f121a
Showing with 29 additions and 2 deletions.
  1. +7 −1 src/ORM/Table.php
  2. +22 −1 tests/TestCase/ORM/QueryRegressionTest.php
@@ -1120,8 +1120,14 @@ public function save(EntityInterface $entity, $options = []) {
*/
protected function _processSave($entity, $options) {
$primary = $entity->extract((array)$this->primaryKey());
if ($primary && $entity->isNew() === null) {
$entity->isNew(!$this->exists($primary));
$alias = $this->alias();
$keys = array_keys($primary);
foreach ($keys as &$pk) {
$pk = "$alias.$pk";
}
$entity->isNew(!$this->exists(array_combine($keys, $primary)));
}
if ($entity->isNew() === null) {
@@ -191,7 +191,7 @@ public function strategyProvider() {
*
* Checks that only relevant associations are passed when saving _joinData
* Tests that _joinData can also save deeper associations
*
*
* @dataProvider strategyProvider
* @param string $strategy
* @return void
@@ -245,4 +245,25 @@ public function testBelongsToManyDeepSave($strategy) {
$this->assertEquals('mark', end($entity->highlights)->author->name);
}
/**
* Tests that no exceptions are generated becuase of ambiguous column names in queries
* during a save operation
*
* @see https://github.com/cakephp/cakephp/issues/3803
* @return void
*/
public function testSaveWithCallbacks() {
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Authors');
$articles->eventManager()->attach(function($event, $query) {
return $query->contain('Authors');
}, 'Model.beforeFind');
$article = $articles->newEntity();
$article->title = 'Foo';
$article->body = 'Bar';
$this->assertSame($article, $articles->save($article));
}
}

0 comments on commit 5b5d537

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