Skip to content
This repository
Browse code

setting defaults in the DBO so you do not need to pass every field po…

…ssible to avoid errors. also adds a test for calling with some missing fields. fixes #1779
  • Loading branch information...
commit 2ad0f8b8db3b08ae762e6f62bb38886a90c1c151 1 parent c754fb2
Carl Sutton authored March 13, 2012
19  lib/Cake/Model/Datasource/DboSource.php
@@ -195,6 +195,23 @@ class DboSource extends DataSource {
195 195
 	);
196 196
 
197 197
 /**
  198
+ * Default fields that are used by the DBO
  199
+ *
  200
+ * @var array
  201
+ */	
  202
+	protected $_queryDefaults = array(
  203
+		'conditions' => array(),
  204
+		'fields' => null,
  205
+		'table' => null,
  206
+		'alias' => null,
  207
+		'order' => null,
  208
+		'limit' => null,
  209
+		'joins' => array(),
  210
+		'group' => null,
  211
+		'offset' => null
  212
+	);
  213
+
  214
+/**
198 215
  * Separator string for virtualField composition
199 216
  *
200 217
  * @var string
@@ -1665,7 +1682,7 @@ public function buildJoinStatement($join) {
1665 1682
  * @see DboSource::renderStatement()
1666 1683
  */
1667 1684
 	public function buildStatement($query, $model) {
1668  
-		$query = array_merge(array('offset' => null, 'joins' => array()), $query);
  1685
+		$query = array_merge($this->_queryDefaults, $query);
1669 1686
 		if (!empty($query['joins'])) {
1670 1687
 			$count = count($query['joins']);
1671 1688
 			for ($i = 0; $i < $count; $i++) {
23  lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -843,4 +843,27 @@ public function testTransactionLogging() {
843 843
 		$log = $db->getLog();
844 844
 		$this->assertEquals($expected, $log['log'][0]);
845 845
 	}
  846
+	
  847
+/**
  848
+ * Test build statement with some fields missing 
  849
+ * 
  850
+ * @return void
  851
+ */
  852
+	public function testBuildStatementDefaults() {
  853
+		$conn = $this->getMock('MockPDO');
  854
+		$db = new DboTestSource;
  855
+		$db->setConnection($conn);
  856
+		$subQuery = $db->buildStatement(
  857
+			array(
  858
+				'fields' => array('DISTINCT(AssetsTag.asset_id)'),
  859
+				'table' => "assets_tags",
  860
+				'alias'=>"AssetsTag",                                          
  861
+				'conditions' => array("Tag.name"=>'foo bar'),
  862
+				'limit' => null,         
  863
+				'group' => "AssetsTag.asset_id"
  864
+			),
  865
+			$this->Model
  866
+		);
  867
+	}
  868
+	
846 869
 }

0 notes on commit 2ad0f8b

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