Skip to content
This repository
Browse code

Extending virtual fields implementation to work on queries with field…

… list
  • Loading branch information...
commit 00c0bb4d62467740cd2ff8fc71120a372ea353db 1 parent 19c91f1
José Lorenzo Rodríguez authored November 11, 2009
26  cake/libs/model/datasources/dbo_source.php
@@ -1758,10 +1758,12 @@ function __scrubQueryData($data) {
1758 1758
 		return $data;
1759 1759
 	}
1760 1760
 
1761  
-	function _constructVirtualFields(&$model,$fields) {
  1761
+	function _constructVirtualFields(&$model,$alias,$fields) {
1762 1762
 		$virtual = array();
1763  
-		foreach ($fields as $name => $expression) {
1764  
-			$virtual[] = $expression . " {$this->alias} {$model->alias}__{$name}";
  1763
+		foreach ($fields as $field) {
  1764
+			$virtualField = $this->name("{$alias}__{$field}");
  1765
+			$expression = $model->virtualFields[$field];
  1766
+			$virtual[] = $expression . " {$this->alias} {$virtualField}";
1765 1767
 		}
1766 1768
 		return $virtual;
1767 1769
 	}
@@ -1790,10 +1792,19 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
1790 1792
 		if (!$quote) {
1791 1793
 			return $fields;
1792 1794
 		}
  1795
+		$virtual = array();
  1796
+		if (!empty($model->virtualFields)) {
  1797
+			$keys =  array_keys($model->virtualFields);
  1798
+			$virtual = ($allFields) ? $keys :  array_intersect($keys,$fields);
  1799
+		}
1793 1800
 		$count = count($fields);
1794 1801
 
1795 1802
 		if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
1796 1803
 			for ($i = 0; $i < $count; $i++) {
  1804
+				if (in_array($fields[$i],$virtual)) {
  1805
+					unset($fields[$i]);
  1806
+					continue;
  1807
+				}
1797 1808
 				if (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){
1798 1809
 					continue;
1799 1810
 				} elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
@@ -1847,12 +1858,9 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
1847 1858
 				}
1848 1859
 			}
1849 1860
 		}
1850  
-		
1851  
-		if (!empty($model->virtualFields)) {
1852  
-			if ($allFields) {
1853  
-				$fields = array_merge($fields,$this->_constructVirtualFields($model,$model->virtualFields));
1854  
-			} else {
1855  
-			}
  1861
+
  1862
+		if (!empty($virtual)) {
  1863
+			$fields = array_merge($fields,$this->_constructVirtualFields($model,$alias,$virtual));
1856 1864
 		}
1857 1865
 		return array_unique($fields);
1858 1866
 	}
30  cake/tests/cases/libs/model/model_read.test.php
@@ -27,19 +27,27 @@
27 27
 class ModelReadTest extends BaseModelTest {
28 28
 
29 29
 	function testVirtualFields() {
30  
-		$this->loadFixtures('Post');
  30
+		$this->loadFixtures('Post','Author');
31 31
 		$Post = ClassRegistry::init('Post');
32 32
 		$Post->virtualFields = array('two' => "1 + 1");
33  
-		$expected = array(
34  
-			'author_id' => 1,
35  
-			'title' => 'First Post',
36  
-			'body' => 'First Post Body',
37  
-			'published' => 'Y',
38  
-			'created' => '2007-03-18 10:39:23',
39  
-			'updated' => '2007-03-18 10:41:31'
40  
-		);
41  
-		debug($Post->find('first'));
42  
-		exit;
  33
+		$result = $Post->find('first');
  34
+		$this->assertEqual($result['Post']['two'],2);
  35
+
  36
+		$Post->Author->virtualFields = array('false' => '1 = 2');
  37
+		$result = $Post->find('first');
  38
+		$this->assertEqual($result['Post']['two'],2);
  39
+		$this->assertEqual($result['Author']['false'],false);
  40
+
  41
+		$result = $Post->find('first',array('fields' => array('author_id')));
  42
+		$this->assertFalse(isset($result['Post']['two']));
  43
+		$this->assertFalse(isset($result['Author']['false']));
  44
+
  45
+		$result = $Post->find('first',array('fields' => array('author_id','two')));
  46
+		$this->assertEqual($result['Post']['two'],2);
  47
+		$this->assertFalse(isset($result['Author']['false']));
  48
+
  49
+		$result = $Post->find('first',array('fields' => array('two')));
  50
+		$this->assertEqual($result['Post']['two'],2);
43 51
 	}
44 52
 
45 53
 /**

0 notes on commit 00c0bb4

Please sign in to comment.
Something went wrong with that request. Please try again.