From 697d76a1bc937b78fbc4619ad4b114a8a4e7b6cd Mon Sep 17 00:00:00 2001 From: AD7six Date: Mon, 16 Feb 2015 11:10:51 +0000 Subject: [PATCH] Ensure table in options not overriden And don't try to set table at all if it's not possible to accurately determine what table the user meant to use --- src/ORM/TableRegistry.php | 2 +- tests/TestCase/ORM/TableRegistryTest.php | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ORM/TableRegistry.php b/src/ORM/TableRegistry.php index 1577c717475..7f78e370211 100644 --- a/src/ORM/TableRegistry.php +++ b/src/ORM/TableRegistry.php @@ -176,7 +176,7 @@ public static function get($alias, array $options = []) if ($className) { $options['className'] = $className; } else { - if (!isset($options['table'])) { + if (!isset($options['table']) && strpos($options['className'], '\\') === false) { list(, $table) = pluginSplit($options['className']); $options['table'] = Inflector::underscore($table); } diff --git a/tests/TestCase/ORM/TableRegistryTest.php b/tests/TestCase/ORM/TableRegistryTest.php index 3e21e196183..2d9b856d68b 100644 --- a/tests/TestCase/ORM/TableRegistryTest.php +++ b/tests/TestCase/ORM/TableRegistryTest.php @@ -184,8 +184,18 @@ public function testGetFallbacks() $result = TableRegistry::get('R2D2', ['className' => 'Droids']); $this->assertInstanceOf('Cake\ORM\Table', $result); - $this->assertEquals('droids', $result->table()); + $this->assertEquals('droids', $result->table(), 'The table should be derived from the className'); $this->assertEquals('R2D2', $result->alias()); + + $result = TableRegistry::get('C3P0', ['className' => 'Droids', 'table' => 'rebels']); + $this->assertInstanceOf('Cake\ORM\Table', $result); + $this->assertEquals('rebels', $result->table(), 'The table should be taken from options'); + $this->assertEquals('C3P0', $result->alias()); + + $result = TableRegistry::get('Stuff', ['className' => 'Cake\ORM\Table']); + $this->assertInstanceOf('Cake\ORM\Table', $result); + $this->assertEquals('stuff', $result->table(), 'The table should be drived from the alias'); + $this->assertEquals('Stuff', $result->alias()); } /**