Adding support for virtual fields in conditions array

This commit is contained in:
José Lorenzo Rodríguez 2009-12-06 20:30:15 -04:30
parent cf359a38b3
commit d06ff5d10a
3 changed files with 32 additions and 2 deletions

View file

@ -2103,6 +2103,11 @@ class DboSource extends DataSource {
}
}
$virtual = false;
if (!empty($model->virtualFields[$key])) {
$key = $model->virtualFields[$key];
$virtual = true;
}
$type = (is_object($model) ? $model->getColumnType($key) : null);
@ -2117,7 +2122,7 @@ class DboSource extends DataSource {
$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);
}

View file

@ -1247,7 +1247,6 @@ class ControllerTest extends CakeTestCase {
function testPaginateOrderVirtualField() {
$Controller =& new Controller();
$Controller->uses = array('ControllerPost', 'ControllerComment');
$Controller->passedArgs[] = '1';
$Controller->params['url'] = array();
$Controller->constructClasses();
$Controller->ControllerPost->virtualFields = array(

View file

@ -62,6 +62,32 @@ class ModelReadTest extends BaseModelTest {
$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");