Fixing more issues with database prefixes

This commit is contained in:
José Lorenzo Rodríguez 2011-02-24 01:31:03 -04:30
parent ea7f0bf900
commit 69b70249da
3 changed files with 35 additions and 17 deletions

View file

@ -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,

View file

@ -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();

View file

@ -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();