Permalink
Browse files

Fixing TranslateBehavior and $tablePrefix. Closes #168

  • Loading branch information...
1 parent 5d35fd8 commit 03200440ed91d5a0c1cac1b0ff4ada8335163ff7 @lorenzo lorenzo committed Jan 24, 2010
@@ -93,8 +93,12 @@ function beforeFind(&$model, $query) {
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';
@@ -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 @@ function testTranslateWithAssociations() {
);
$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);
+ }
}
?>
@@ -2791,6 +2791,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.
*
* @package cake
@@ -2827,6 +2856,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.
*
* @package cake
@@ -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')
+ );
+}
+?>

0 comments on commit 0320044

Please sign in to comment.