Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing issue where table name was not using fully qualified table nam…

…es, causing issues with models using table prefixes. Tests added. Fixes #623
  • Loading branch information...
commit af6435ece8ced3a83ba9aa0a1ed076cf71d746e1 1 parent 1c06478
@markstory markstory authored
View
18 cake/libs/model/cake_schema.php
@@ -249,10 +249,10 @@ function read($options = array()) {
if (in_array($table, $currentTables)) {
$key = array_search($table, $currentTables);
- if (empty($tables[$Object->table])) {
- $tables[$Object->table] = $this->__columns($Object);
- $tables[$Object->table]['indexes'] = $db->index($Object);
- $tables[$Object->table]['tableParameters'] = $db->readTableParameters($table);
+ if (empty($tables[$table])) {
+ $tables[$table] = $this->__columns($Object);
+ $tables[$table]['indexes'] = $db->index($Object);
+ $tables[$table]['tableParameters'] = $db->readTableParameters($table);
unset($currentTables[$key]);
}
if (!empty($Object->hasAndBelongsToMany)) {
@@ -261,12 +261,12 @@ function read($options = array()) {
$class = $assocData['with'];
}
if (is_object($Object->$class)) {
- $table = $db->fullTableName($Object->$class, false);
- if (in_array($table, $currentTables)) {
+ $withTable = $db->fullTableName($Object->$class, false);
+ if (in_array($withTable, $currentTables)) {
$key = array_search($table, $currentTables);
- $tables[$Object->$class->table] = $this->__columns($Object->$class);
- $tables[$Object->$class->table]['indexes'] = $db->index($Object->$class);
- $tables[$Object->$class->table]['tableParameters'] = $db->readTableParameters($table);
+ $tables[$withTable] = $this->__columns($Object->$class);
+ $tables[$withTable]['indexes'] = $db->index($Object->$class);
+ $tables[$withTable]['tableParameters'] = $db->readTableParameters($withTable);
unset($currentTables[$key]);
}
}
View
51 cake/tests/cases/libs/model/cake_schema.test.php
@@ -449,6 +449,33 @@ class SchemaCrossDatabaseFixture extends CakeTestFixture {
}
/**
+ * SchemaPrefixAuthUser class
+ *
+ * @package cake
+ * @subpackage cake.tests.cases.libs.model
+ */
+class SchemaPrefixAuthUser extends CakeTestModel {
+/**
+ * name property
+ *
+ * @var string
+ */
+ var $name = 'SchemaPrefixAuthUser';
+/**
+ * table prefix
+ *
+ * @var string
+ */
+ var $tablePrefix = 'auth_';
+/**
+ * useTable
+ *
+ * @var string
+ */
+ var $useTable = 'users';
+}
+
+/**
* CakeSchemaTest
*
* @package cake
@@ -463,7 +490,7 @@ class CakeSchemaTest extends CakeTestCase {
* @access public
*/
var $fixtures = array(
- 'core.post', 'core.tag', 'core.posts_tag', 'core.test_plugin_comment',
+ 'core.post', 'core.tag', 'core.posts_tag', 'core.test_plugin_comment',
'core.datatype', 'core.auth_user', 'core.author',
'core.test_plugin_article', 'core.user', 'core.comment'
);
@@ -523,7 +550,6 @@ function testSchemaRead() {
$expected = array('comments', 'datatypes', 'posts', 'posts_tags', 'tags');
$this->assertEqual(array_keys($read['tables']), $expected);
-
foreach ($read['tables'] as $table => $fields) {
$this->assertEqual(array_keys($fields), array_keys($this->Schema->tables[$table]));
}
@@ -552,6 +578,25 @@ function testSchemaRead() {
}
/**
+ * test read() with tablePrefix properties.
+ *
+ * @return void
+ */
+ function testSchemaReadWithTablePrefix() {
+ $model =& new SchemaPrefixAuthUser();
+
+ $Schema =& new CakeSchema();
+ $read = $Schema->read(array(
+ 'connection' => 'test_suite',
+ 'name' => 'TestApp',
+ 'models' => array('SchemaPrefixAuthUser')
+ ));
+ unset($read['tables']['missing']);
+ $this->assertTrue(isset($read['tables']['auth_users']), 'auth_users key missing %s');
+
+ }
+
+/**
* test reading schema from plugins.
*
* @return void
@@ -575,7 +620,7 @@ function testSchemaReadWithPlugins() {
$this->assertTrue(isset($read['tables']['test_plugin_comments']));
$this->assertTrue(isset($read['tables']['posts']));
$this->assertEqual(count($read['tables']), 4);
-
+
App::build();
}
Please sign in to comment.
Something went wrong with that request. Please try again.