Skip to content

Commit

Permalink
Removing hardcoded '__' for virtualField separators. Making it an ins…
Browse files Browse the repository at this point in the history
…tance property instead. This allows the customization of the separator if needed. Tests added for DboMysql. Refs #655, #730
  • Loading branch information
markstory committed May 22, 2010
1 parent 23d4baf commit 29f2223
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cake/libs/model/datasources/dbo/dbo_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions cake/libs/model/datasources/dbo_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ class DboSource extends DataSource {
'rollback' => 'ROLLBACK'
);

/**
* Separator string for virtualField composition
*
* @var string
*/
var $virtualFieldSeparator = '__';

/**
* List of table engine specific parameters used on table creating
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}";
}
Expand Down
18 changes: 18 additions & 0 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.