Skip to content

Commit

Permalink
Adding support for virtual fields in conditions array
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 11, 2009
1 parent cf359a3 commit d06ff5d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cake/libs/model/datasources/dbo_source.php
Expand Up @@ -2103,6 +2103,11 @@ function __parseKey($model, $key, $value) {
}
}

$virtual = false;
if (!empty($model->virtualFields[$key])) {
$key = $model->virtualFields[$key];
$virtual = true;
}

$type = (is_object($model) ? $model->getColumnType($key) : null);

Expand All @@ -2117,7 +2122,7 @@ function __parseKey($model, $key, $value) {

$value = $this->value($value, $type);

if ($key !== '?') {
if (!$virtual && $key !== '?') {
$isKey = (strpos($key, '(') !== false || strpos($key, ')') !== false);
$key = $isKey ? $this->__quoteFields($key) : $this->name($key);
}
Expand Down
1 change: 0 additions & 1 deletion cake/tests/cases/libs/controller/controller.test.php
Expand Up @@ -1247,7 +1247,6 @@ function testRequestHandlerPrefers(){
function testPaginateOrderVirtualField() {
$Controller =& new Controller();
$Controller->uses = array('ControllerPost', 'ControllerComment');
$Controller->passedArgs[] = '1';
$Controller->params['url'] = array();
$Controller->constructClasses();
$Controller->ControllerPost->virtualFields = array(
Expand Down
26 changes: 26 additions & 0 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -62,6 +62,32 @@ 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' => $dbo->name('Post.id') . ' + 1');
$result = $Post->find('first',array(
'conditions' => array('other_field' => 3),
'limit' => 1
));
$this->assertEqual($result['Post']['id'],2);

ClassRegistry::flush();
$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model');
$Writing->virtualFields = array('two' => "1 + 1");
Expand Down

0 comments on commit d06ff5d

Please sign in to comment.