From 2ad0f8b8db3b08ae762e6f62bb38886a90c1c151 Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Tue, 13 Mar 2012 17:37:17 +0000 Subject: [PATCH] setting defaults in the DBO so you do not need to pass every field possible to avoid errors. also adds a test for calling with some missing fields. fixes #1779 --- lib/Cake/Model/Datasource/DboSource.php | 19 ++++++++++++++- .../Case/Model/Datasource/DboSourceTest.php | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 82dfeeb1e..57b198d67 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -194,6 +194,23 @@ class DboSource extends DataSource { 'rollback' => 'ROLLBACK' ); +/** + * Default fields that are used by the DBO + * + * @var array + */ + protected $_queryDefaults = array( + 'conditions' => array(), + 'fields' => null, + 'table' => null, + 'alias' => null, + 'order' => null, + 'limit' => null, + 'joins' => array(), + 'group' => null, + 'offset' => null + ); + /** * Separator string for virtualField composition * @@ -1665,7 +1682,7 @@ class DboSource extends DataSource { * @see DboSource::renderStatement() */ public function buildStatement($query, $model) { - $query = array_merge(array('offset' => null, 'joins' => array()), $query); + $query = array_merge($this->_queryDefaults, $query); if (!empty($query['joins'])) { $count = count($query['joins']); for ($i = 0; $i < $count; $i++) { diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index a8a27f1d6..59fb89709 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -843,4 +843,27 @@ class DboSourceTest extends CakeTestCase { $log = $db->getLog(); $this->assertEquals($expected, $log['log'][0]); } + +/** + * Test build statement with some fields missing + * + * @return void + */ + public function testBuildStatementDefaults() { + $conn = $this->getMock('MockPDO'); + $db = new DboTestSource; + $db->setConnection($conn); + $subQuery = $db->buildStatement( + array( + 'fields' => array('DISTINCT(AssetsTag.asset_id)'), + 'table' => "assets_tags", + 'alias'=>"AssetsTag", + 'conditions' => array("Tag.name"=>'foo bar'), + 'limit' => null, + 'group' => "AssetsTag.asset_id" + ), + $this->Model + ); + } + }