Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing more issues with database prefixes
  • Loading branch information
lorenzo committed Feb 24, 2011
1 parent ea7f0bf commit 69b7024
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
12 changes: 8 additions & 4 deletions cake/libs/model/behaviors/translate.php
Expand Up @@ -90,20 +90,24 @@ public function beforeFind($model, $query) {
if (empty($locale)) {
return $query;
}
$db = ConnectionManager::getDataSource($model->useDbConfig);
$db = $model->getDataSource();
$RuntimeModel = $this->translateModel($model);
if (!empty($RuntimeModel->tablePrefix)) {
$tablePrefix = $RuntimeModel->tablePrefix;
} else {
$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,
Expand Down Expand Up @@ -149,7 +153,7 @@ public function beforeFind($model, $query) {
$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,
Expand All @@ -166,7 +170,7 @@ public function beforeFind($model, $query) {
$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,
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/model/cake_schema.php
Expand Up @@ -240,7 +240,7 @@ public function read($options = array()) {
}

$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) {
Expand Down Expand Up @@ -575,7 +575,7 @@ public function __values($values) {
* @return array Formatted columns
*/
public function __columns(&$Obj) {
$db = ConnectionManager::getDataSource($Obj->useDbConfig);
$db = $Obj->getDataSource();
$fields = $Obj->schema(true);

$columns = $props = array();
Expand Down
28 changes: 21 additions & 7 deletions cake/tests/cases/libs/model/cake_schema.test.php
Expand Up @@ -589,22 +589,33 @@ function testSchemaRead() {
$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')
'models' => array('SchemaComment', 'SchemaTag', 'SchemaPost')
));
$this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix %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('SchemaComment', 'SchemaTag', 'SchemaPost')
'models' => array('SchemaPost')
));
$this->assertFalse(isset($read['tables']['missing']['posts_tags']), 'Join table marked as missing %s');

$this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix');
}

/**
Expand All @@ -613,6 +624,9 @@ function testSchemaRead() {
* @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();
Expand Down

0 comments on commit 69b7024

Please sign in to comment.