Skip to content

Commit

Permalink
Trigger an exception when alias names do not match.
Browse files Browse the repository at this point in the history
Raising an exception in the scenario where a developer mis-types an
association name prevents accidental alias mistakes, and more
importantly prevents data from being returned as an array when it should
be entity instances.

Refs #7110
  • Loading branch information
markstory committed Jul 28, 2015
1 parent 271ccb3 commit c6eca2f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/ORM/EagerLoader.php
Expand Up @@ -385,6 +385,13 @@ protected function _normalizeContain(Table $parent, $alias, $options, $paths)
sprintf('%s is not associated with %s', $parent->alias(), $alias)
);
}
if ($instance->alias() !== $alias) {
throw new InvalidArgumentException(sprintf(
"You have contained '%s' but that association was bound as '%s'.",
$alias,
$instance->alias()
));
}

$paths += ['aliasPath' => '', 'propertyPath' => '', 'root' => $alias];
$paths['aliasPath'] .= '.' . $alias;
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/ORM/EagerLoaderTest.php
Expand Up @@ -375,6 +375,21 @@ public function testContainToFieldsDefault()
$this->assertEquals($expected, $select);
}

/**
* Check that normalizing contains checks alias names.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You have contained 'Clients' but that association was bound as 'clients'
* @return void
*/
public function testNormalizedChecksAliasNames()
{
$contains = ['Clients'];
$loader = new EagerLoader;
$loader->contain($contains);
$loader->normalized($this->table);
}

/**
* Tests that the path for gettings to a deep assocition is materialized in an
* array key
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -3277,7 +3277,7 @@ public function testSaveBelongsToManyJoinDataOnExistingRecord()
public function testSaveBelongsToManyJoinData()
{
$articles = TableRegistry::get('Articles');
$article = $articles->get(1, ['contain' => ['Tags']]);
$article = $articles->get(1, ['contain' => ['tags']]);
$data = [
'tags' => [
['id' => 1, '_joinData' => ['highlighted' => 1]],
Expand Down Expand Up @@ -3376,15 +3376,15 @@ public function testSaveBelongsToManyDeleteAllLinks()
'saveStrategy' => 'replace',
]);

$entity = $table->get(1, ['contain' => 'Tags']);
$entity = $table->get(1, ['contain' => 'tags']);
$this->assertCount(2, $entity->tags, 'Fixture data did not change.');

$entity->tags = [];
$result = $table->save($entity);
$this->assertSame($result, $entity);
$this->assertSame([], $entity->tags, 'No tags on the entity.');

$entity = $table->get(1, ['contain' => 'Tags']);
$entity = $table->get(1, ['contain' => 'tags']);
$this->assertSame([], $entity->tags, 'No tags in the db either.');
}

Expand All @@ -3401,7 +3401,7 @@ public function testSaveBelongsToManyDeleteSomeLinks()
'saveStrategy' => 'replace',
]);

$entity = $table->get(1, ['contain' => 'Tags']);
$entity = $table->get(1, ['contain' => 'tags']);
$this->assertCount(2, $entity->tags, 'Fixture data did not change.');

$tag = new \Cake\ORM\Entity([
Expand All @@ -3413,7 +3413,7 @@ public function testSaveBelongsToManyDeleteSomeLinks()
$this->assertCount(1, $entity->tags, 'Only one tag left.');
$this->assertEquals($tag, $entity->tags[0]);

$entity = $table->get(1, ['contain' => 'Tags']);
$entity = $table->get(1, ['contain' => 'tags']);
$this->assertCount(1, $entity->tags, 'Only one tag in the db.');
$this->assertEquals($tag->id, $entity->tags[0]->id);
}
Expand All @@ -3425,8 +3425,8 @@ public function testSaveBelongsToManyDeleteSomeLinks()
*/
public function testSaveBelongsToManyIgnoreNonEntityData()
{
$articles = TableRegistry::get('Articles');
$article = $articles->get(1, ['contain' => ['Tags']]);
$articles = TableRegistry::get('articles');
$article = $articles->get(1, ['contain' => ['tags']]);
$article->tags = [
'_ids' => [2, 1]
];
Expand Down

0 comments on commit c6eca2f

Please sign in to comment.