diff --git a/cake/libs/model/behaviors/translate.php b/cake/libs/model/behaviors/translate.php index 3f0c462d5..ff7c9bdc5 100644 --- a/cake/libs/model/behaviors/translate.php +++ b/cake/libs/model/behaviors/translate.php @@ -90,7 +90,7 @@ class TranslateBehavior extends ModelBehavior { if (empty($locale)) { return $query; } - $db = ConnectionManager::getDataSource($model->useDbConfig); + $db = $model->getDataSource(); $RuntimeModel = $this->translateModel($model); if (!empty($RuntimeModel->tablePrefix)) { $tablePrefix = $RuntimeModel->tablePrefix; @@ -98,12 +98,16 @@ class TranslateBehavior extends ModelBehavior { $tablePrefix = $db->config['prefix']; } + if ($tablePrefix == $db->config['prefix']) { + $tablePrefix = null; + } + if (is_string($query['fields']) && 'COUNT(*) AS '.$db->name('count') == $query['fields']) { $query['fields'] = 'COUNT(DISTINCT('.$db->name($model->alias . '.' . $model->primaryKey) . ')) ' . $db->alias . 'count'; $query['joins'][] = array( 'type' => 'INNER', 'alias' => $RuntimeModel->alias, - 'table' => $db->name($tablePrefix . $RuntimeModel->useTable), + 'table' => $db->fullTableName($tablePrefix . $RuntimeModel->useTable), 'conditions' => array( $model->alias . '.' . $model->primaryKey => $db->identifier($RuntimeModel->alias.'.foreign_key'), $RuntimeModel->alias.'.model' => $model->name, @@ -149,7 +153,7 @@ class TranslateBehavior extends ModelBehavior { $query['joins'][] = array( 'type' => 'LEFT', 'alias' => 'I18n__'.$field.'__'.$_locale, - 'table' => $db->name($tablePrefix . $RuntimeModel->useTable), + 'table' => $db->fullTableName($tablePrefix . $RuntimeModel->useTable), 'conditions' => array( $model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"), 'I18n__'.$field.'__'.$_locale.'.model' => $model->name, @@ -166,7 +170,7 @@ class TranslateBehavior extends ModelBehavior { $query['joins'][] = array( 'type' => 'LEFT', 'alias' => 'I18n__'.$field, - 'table' => $db->name($tablePrefix . $RuntimeModel->useTable), + 'table' => $db->fullTableName($tablePrefix . $RuntimeModel->useTable), 'conditions' => array( $model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"), 'I18n__'.$field.'.model' => $model->name, diff --git a/cake/libs/model/cake_schema.php b/cake/libs/model/cake_schema.php index 3542a6300..5579540b0 100644 --- a/cake/libs/model/cake_schema.php +++ b/cake/libs/model/cake_schema.php @@ -240,7 +240,7 @@ class CakeSchema extends Object { } $Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection)); - + $db = $Object->getDataSource(); if (is_object($Object) && $Object->useTable !== false) { $fulltable = $table = $db->fullTableName($Object, false); if ($prefix && strpos($table, $prefix) !== 0) { @@ -575,7 +575,7 @@ class CakeSchema extends Object { * @return array Formatted columns */ public function __columns(&$Obj) { - $db = ConnectionManager::getDataSource($Obj->useDbConfig); + $db = $Obj->getDataSource(); $fields = $Obj->schema(true); $columns = $props = array(); diff --git a/cake/tests/cases/libs/model/cake_schema.test.php b/cake/tests/cases/libs/model/cake_schema.test.php index 69fb920d2..78890fbf7 100644 --- a/cake/tests/cases/libs/model/cake_schema.test.php +++ b/cake/tests/cases/libs/model/cake_schema.test.php @@ -589,22 +589,33 @@ class CakeSchemaTest extends CakeTestCase { $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false)); $this->assertTrue(empty($read['tables'])); - $SchemaPost = ClassRegistry::init('SchemaPost'); - $SchemaPost->table = 'sts'; - $SchemaPost->tablePrefix = 'po'; - $read = $this->Schema->read(array( - 'connection' => 'test', - 'name' => 'TestApp', - 'models' => array('SchemaPost') - )); - $this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix %s'); - $read = $this->Schema->read(array( 'connection' => 'test', 'name' => 'TestApp', 'models' => array('SchemaComment', 'SchemaTag', 'SchemaPost') )); - $this->assertFalse(isset($read['tables']['missing']['posts_tags']), 'Join table marked as missing %s'); + $this->assertFalse(isset($read['tables']['missing']['posts_tags']), 'Join table marked as missing'); + } + +/** + * testSchemaReadWithOddTablePrefix method + * + * @access public + * @return void + */ + function testSchemaReadWithOddTablePrefix() { + $config = ConnectionManager::getDataSource('test')->config; + $this->skipIf(!empty($config['prefix']), 'This test can not be executed with datasource prefix set'); + $SchemaPost = ClassRegistry::init('SchemaPost'); + $SchemaPost->tablePrefix = 'po'; + $SchemaPost->useTable = 'sts'; + $read = $this->Schema->read(array( + 'connection' => 'test', + 'name' => 'TestApp', + 'models' => array('SchemaPost') + )); + + $this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix'); } /** @@ -613,6 +624,9 @@ class CakeSchemaTest extends CakeTestCase { * @return void */ function testSchemaReadWithTablePrefix() { + $config = ConnectionManager::getDataSource('test')->config; + $this->skipIf(!empty($config['prefix']), 'This test can not be executed with datasource prefix set'); + $model = new SchemaPrefixAuthUser(); $Schema = new CakeSchema();