Permalink
Browse files

Adding support for virtual fields in conditions array

  • Loading branch information...
1 parent cf359a3 commit d06ff5d10acba144e8b242318f86420097233948 @lorenzo lorenzo committed Dec 7, 2009
@@ -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);
@@ -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);
}
@@ -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(
@@ -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");

0 comments on commit d06ff5d

Please sign in to comment.