Fixing TranslateBehavior and $tablePrefix. Closes #168

This commit is contained in:
José Lorenzo Rodríguez 2010-01-24 18:27:08 -04:30
parent 5d35fd8d38
commit 03200440ed
4 changed files with 182 additions and 2 deletions

View file

@ -93,8 +93,12 @@ class TranslateBehavior extends ModelBehavior {
return $query;
}
$db =& ConnectionManager::getDataSource($model->useDbConfig);
$tablePrefix = $db->config['prefix'];
$RuntimeModel =& $this->translateModel($model);
if (!empty($RuntimeModel->tablePrefix)) {
$tablePrefix = $RuntimeModel->tablePrefix;
} else {
$tablePrefix = $db->config['prefix'];
}
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';

View file

@ -52,7 +52,8 @@ class TranslateBehaviorTest extends CakeTestCase {
*/
var $fixtures = array(
'core.translated_item', 'core.translate', 'core.translate_table',
'core.translated_article', 'core.translate_article', 'core.user', 'core.comment', 'core.tag', 'core.articles_tag'
'core.translated_article', 'core.translate_article', 'core.user', 'core.comment', 'core.tag', 'core.articles_tag',
'core.translate_with_prefix'
);
/**
* endTest method
@ -835,5 +836,26 @@ class TranslateBehaviorTest extends CakeTestCase {
);
$this->assertEqual($result, $expected);
}
/**
* testTranslateTableWithPrefix method
* Tests that is possible to have a translation model with a custom tablePrefix
*
* @access public
* @return void
*/
function testTranslateTableWithPrefix() {
$this->loadFixtures('TranslateWithPrefix', 'TranslatedItem');
$TestModel =& new TranslatedItem2;
$TestModel->locale = 'eng';
$result = $TestModel->read(null, 1);
$expected = array('TranslatedItem' => array(
'id' => 1,
'slug' => 'first_translated',
'locale' => 'eng',
'content' => 'Content #1',
'title' => 'Title #1'
));
$this->assertEqual($result, $expected);
}
}
?>

View file

@ -2790,6 +2790,35 @@ class TranslateTestModel extends CakeTestModel {
*/
var $displayField = 'field';
}
/**
* TranslateTestModel class.
*
* @package cake
* @subpackage cake.tests.cases.libs.model
*/
class TranslateWithPrefix extends CakeTestModel {
/**
* name property
*
* @var string 'TranslateTestModel'
* @access public
*/
var $name = 'TranslateWithPrefix';
/**
* tablePrefix property
*
* @var string 'i18n'
* @access public
*/
var $tablePrefix = 'i18n_';
/**
* displayField property
*
* @var string 'field'
* @access public
*/
var $displayField = 'field';
}
/**
* TranslatedItem class.
*
@ -2826,6 +2855,42 @@ class TranslatedItem extends CakeTestModel {
*/
var $translateModel = 'TranslateTestModel';
}
/**
* TranslatedItem class.
*
* @package cake
* @subpackage cake.tests.cases.libs.model
*/
class TranslatedItem2 extends CakeTestModel {
/**
* name property
*
* @var string 'TranslatedItem'
* @access public
*/
var $name = 'TranslatedItem';
/**
* cacheQueries property
*
* @var bool false
* @access public
*/
var $cacheQueries = false;
/**
* actsAs property
*
* @var array
* @access public
*/
var $actsAs = array('Translate' => array('content', 'title'));
/**
* translateModel property
*
* @var string 'TranslateTestModel'
* @access public
*/
var $translateModel = 'TranslateWithPrefix';
}
/**
* TranslatedItemWithTable class.
*

View file

@ -0,0 +1,89 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.fixtures
* @since CakePHP(tm) v 1.2.0.5669
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/**
* Short description for class.
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class TranslateWithPrefixFixture extends CakeTestFixture {
/**
* name property
*
* @var string 'Translate'
* @access public
*/
var $name = 'TranslateWithPrefix';
/**
* table property
*
* @var string 'i18n'
* @access public
*/
var $table = 'i18n_translate_with_prefixes';
/**
* fields property
*
* @var array
* @access public
*/
var $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'locale' => array('type' => 'string', 'length' => 6, 'null' => false),
'model' => array('type' => 'string', 'null' => false),
'foreign_key' => array('type' => 'integer', 'null' => false),
'field' => array('type' => 'string', 'null' => false),
'content' => array('type' => 'text')
);
/**
* records property
*
* @var array
* @access public
*/
var $records = array(
array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'),
array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1'),
array('id' => 7, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Title #2'),
array('id' => 8, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Content #2'),
array('id' => 9, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Titel #2'),
array('id' => 10, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Inhalt #2'),
array('id' => 11, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Titulek #2'),
array('id' => 12, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Obsah #2'),
array('id' => 13, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Title #3'),
array('id' => 14, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Content #3'),
array('id' => 15, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titel #3'),
array('id' => 16, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Inhalt #3'),
array('id' => 17, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titulek #3'),
array('id' => 18, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Obsah #3')
);
}
?>