Skip to content

Commit

Permalink
Fixing issue where table name was not using fully qualified table nam…
Browse files Browse the repository at this point in the history
…es, causing issues with models using table prefixes. Tests added. Fixes #623
  • Loading branch information
markstory committed Apr 29, 2010
1 parent 2420bce commit 7ea8640
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
18 changes: 9 additions & 9 deletions cake/libs/model/cake_schema.php
Expand Up @@ -244,10 +244,10 @@ public 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)) {
Expand All @@ -256,12 +256,12 @@ public 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]);
}
}
Expand Down
49 changes: 47 additions & 2 deletions cake/tests/cases/libs/model/cake_schema.test.php
Expand Up @@ -448,6 +448,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
*
Expand Down Expand Up @@ -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]));
}
Expand Down Expand Up @@ -551,6 +577,25 @@ function testSchemaRead() {
$this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix %s');
}

/**
* 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.
*
Expand All @@ -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();
}

Expand Down

0 comments on commit 7ea8640

Please sign in to comment.