From 5e26d282a10884ce685d5ec3c3b921c7db54096e Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 30 Sep 2010 23:22:57 -0400 Subject: [PATCH] Fixing connection specific schema generation Fixing missing table errors when reading schema for specific connections. Tests updated to check that tables not on a connection are never touched. Fixes #1106 --- cake/libs/model/cake_schema.php | 16 ++++++++++++---- cake/tests/cases/libs/model/cake_schema.test.php | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cake/libs/model/cake_schema.php b/cake/libs/model/cake_schema.php index a1ea24acef0..6e2d50867db 100644 --- a/cake/libs/model/cake_schema.php +++ b/cake/libs/model/cake_schema.php @@ -234,17 +234,25 @@ function read($options = array()) { if (is_array($models)) { foreach ($models as $model) { + $importModel = $model; if (isset($this->plugin)) { - $model = $this->plugin . '.' . $model; + $importModel = $this->plugin . '.' . $model; } + if (!App::import('Model', $importModel)) { + continue; + } + $vars = get_class_vars($model); + if (empty($vars['useDbConfig']) || $vars['useDbConfig'] != $connection) { + continue; + } + if (PHP5) { - $Object = ClassRegistry::init(array('class' => $model, 'ds' => null)); + $Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection)); } else { - $Object =& ClassRegistry::init(array('class' => $model, 'ds' => null)); + $Object =& ClassRegistry::init(array('class' => $model, 'ds' => $connection)); } if (is_object($Object) && $Object->useTable !== false) { - $Object->setDataSource($connection); $table = $db->fullTableName($Object, false); if (in_array($table, $currentTables)) { $key = array_search($table, $currentTables); diff --git a/cake/tests/cases/libs/model/cake_schema.test.php b/cake/tests/cases/libs/model/cake_schema.test.php index 3a769356ab3..3dc5fc9f52d 100644 --- a/cake/tests/cases/libs/model/cake_schema.test.php +++ b/cake/tests/cases/libs/model/cake_schema.test.php @@ -658,17 +658,17 @@ function testSchemaReadWithCrossDatabase() { 'name' => 'TestApp', 'models' => array('SchemaCrossDatabase', 'SchemaPost') )); - unset($read['tables']['missing']); $this->assertTrue(isset($read['tables']['posts'])); - $this->assertFalse(isset($read['tables']['cross_database'])); + $this->assertFalse(isset($read['tables']['cross_database']), 'Cross database should not appear'); + $this->assertFalse(isset($read['tables']['missing']['cross_database']), 'Cross database should not appear'); $read = $this->Schema->read(array( 'connection' => 'test2', 'name' => 'TestApp', 'models' => array('SchemaCrossDatabase', 'SchemaPost') )); - unset($read['tables']['missing']); - $this->assertFalse(isset($read['tables']['posts'])); + $this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear'); + $this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear'); $this->assertTrue(isset($read['tables']['cross_database'])); $fixture->drop($db2);