Skip to content

Commit

Permalink
Quoting virtual fields in automatically
Browse files Browse the repository at this point in the history
Updating test cases
  • Loading branch information
lorenzo committed Dec 11, 2009
1 parent 8c4cad8 commit c65e2f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
10 changes: 5 additions & 5 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1639,7 +1639,7 @@ function calculate(&$model, $func, $params = array()) {
$params[1] = 'count';
}
if (is_object($model) && $model->isVirtualField($params[0])){
$arg = $model->getVirtualField($params[0]);
$arg = $this->__quoteFields($model->getVirtualField($params[0]));
} else {
$arg = $this->name($params[0]);
}
Expand All @@ -1650,7 +1650,7 @@ function calculate(&$model, $func, $params = array()) {
$params[1] = $params[0];
}
if (is_object($model) && $model->isVirtualField($params[0])) {
$arg = $model->getVirtualField($params[0]);
$arg = $this->__quoteFields($model->getVirtualField($params[0]));
} else {
$arg = $this->name($params[0]);
}
Expand Down Expand Up @@ -1787,7 +1787,7 @@ function _constructVirtualFields(&$model,$alias,$fields) {
$virtual = array();
foreach ($fields as $field) {
$virtualField = $this->name("{$alias}__{$field}");
$expression = $model->getVirtualField($field);
$expression = $this->__quoteFields($model->getVirtualField($field));
$virtual[] = $expression . " {$this->alias} {$virtualField}";
}
return $virtual;
Expand Down Expand Up @@ -2068,7 +2068,7 @@ function __parseKey($model, $key, $value) {

$virtual = false;
if (is_object($model) && $model->isVirtualField($key)) {
$key = $model->getVirtualField($key);
$key = $this->__quoteFields($model->getVirtualField($key));
$virtual = true;
}

Expand Down Expand Up @@ -2241,7 +2241,7 @@ function order($keys, $direction = 'ASC', &$model = null) {
$key = trim($key);
if (!preg_match('/\s/', $key) && !strpos($key,'.')) {
if (is_object($model) && $model->isVirtualField($key)) {
$key = $model->getVirtualField($key);
$key = $this->__quoteFields($model->getVirtualField($key));
} else {
$key = $this->name($key);
}
Expand Down
40 changes: 38 additions & 2 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -26,7 +26,6 @@
*/
class ModelReadTest extends BaseModelTest {


/**
* testFetchingNonUniqueFKJoinTableRecords()
*
Expand Down Expand Up @@ -7172,7 +7171,7 @@ function testFindQueryTypeInCallbacks() {
* @access public
* @return void
*/
function testVirtualFields() {
function testVirtualFields() {
$this->loadFixtures('Post','Author');
$Post = ClassRegistry::init('Post');
$Post->virtualFields = array('two' => "1 + 1");
Expand All @@ -7199,11 +7198,48 @@ function testVirtualFields() {
$result = $Post->field('two');
$this->assertEqual($result,2);

$result = $Post->find('first',array(
'conditions' => array('two' => 2),
'limit' => 1
));
$this->assertEqual($result['Post']['two'],2);

$result = $Post->find('first',array(
'conditions' => array('two <' => 3),
'limit' => 1
));
$this->assertEqual($result['Post']['two'],2);

$result = $Post->find('first',array(
'conditions' => array('NOT' => array('two >' => 3)),
'limit' => 1
));
$this->assertEqual($result['Post']['two'],2);

$dbo =& $Post->getDataSource();
$Post->virtualFields = array('other_field' => 'Post.id + 1');
$result = $Post->find('first',array(
'conditions' => array('other_field' => 3),
'limit' => 1
));
$this->assertEqual($result['Post']['id'],2);

$Post->virtualFields = array('other_field' => 'Post.id + 1');
$result = $Post->find('all',array(
'fields' => array($dbo->calculate($Post,'max',array('other_field')))
));
$this->assertEqual($result[0][0]['other_field'],4);

ClassRegistry::flush();
$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model');
$Writing->virtualFields = array('two' => "1 + 1");
$result = $Writing->find('first');
$this->assertEqual($result['Writing']['two'],2);

$Post->create();
$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
$result = $Post->field('other_field');
$this->assertEqual($result,4);
}
}
?>

0 comments on commit c65e2f3

Please sign in to comment.