Browse files

Removing hardcoded '__' for virtualField separators. Making it an ins…

…tance property instead. This allows the customization of the separator if needed. Tests added for DboMysql. Refs #655, #730
  • Loading branch information...
1 parent 23d4baf commit 29f2223c6de2d1ce99dc83625f7449d97fbe1799 @markstory markstory committed May 22, 2010
View
2 cake/libs/model/datasources/dbo/dbo_mysql.php
@@ -728,7 +728,7 @@ function resultSet(&$results) {
while ($j < $numFields) {
$column = mysql_fetch_field($results, $j);
- if (!empty($column->table) && strpos($column->name, '__') === false) {
+ if (!empty($column->table) && strpos($column->name, $this->virtualFieldSeparator) === false) {
$this->map[$index++] = array($column->table, $column->name);
} else {
$this->map[$index++] = array(0, $column->name);
View
13 cake/libs/model/datasources/dbo_source.php
@@ -98,6 +98,13 @@ class DboSource extends DataSource {
);
/**
+ * Separator string for virtualField composition
+ *
+ * @var string
+ */
+ var $virtualFieldSeparator = '__';
+
+/**
* List of table engine specific parameters used on table creating
*
* @var array
@@ -432,10 +439,10 @@ function fetchAll($sql, $cache = true, $modelName = null) {
function fetchVirtualField(&$result) {
if (isset($result[0]) && is_array($result[0])) {
foreach ($result[0] as $field => $value) {
- if (strpos($field, '__') === false) {
+ if (strpos($field, $this->virtualFieldSeparator) === false) {
continue;
}
- list($alias, $virtual) = explode('__', $field);
+ list($alias, $virtual) = explode($this->virtualFieldSeparator, $field);
if (!ClassRegistry::isKeySet($alias)) {
return;
@@ -1902,7 +1909,7 @@ function __scrubQueryData($data) {
function _constructVirtualFields(&$model, $alias, $fields) {
$virtual = array();
foreach ($fields as $field) {
- $virtualField = $this->name("{$alias}__{$field}");
+ $virtualField = $this->name($alias . $this->virtualFieldSeparator . $field);
$expression = $this->__quoteFields($model->getVirtualField($field));
$virtual[] = '(' .$expression . ") {$this->alias} {$virtualField}";
}
View
18 cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
@@ -759,4 +759,22 @@ function testGetCharsetName() {
$this->assertEqual($result, 'cp1250');
}
+/**
+ * test that changing the virtualFieldSeparator allows for __ fields.
+ *
+ * @return void
+ */
+ function testVirtualFieldSeparators() {
+ $model =& new CakeTestModel(array('table' => 'binary_tests', 'ds' => 'test_suite', 'name' => 'BinaryTest'));
+ $model->virtualFields = array(
+ 'other__field' => 'SUM(id)'
+ );
+
+ $this->db->virtualFieldSeparator = '_$_';
+ $result = $this->db->fields($model, null, array('data', 'other__field'));
+ $expected = array('`BinaryTest`.`data`', '(SUM(id)) AS BinaryTest_$_other__field');
+ $this->assertEqual($result, $expected);
+
+ }
+
}

0 comments on commit 29f2223

Please sign in to comment.