Skip to content

Commit

Permalink
setting defaults in the DBO so you do not need to pass every field po…
Browse files Browse the repository at this point in the history
…ssible to avoid errors. also adds a test for calling with some missing fields. fixes #1779
  • Loading branch information
dogmatic69 committed Mar 13, 2012
1 parent c754fb2 commit 2ad0f8b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -1665,7 +1682,7 @@ public function buildJoinStatement($join) {
* @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++) {
Expand Down
23 changes: 23 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -843,4 +843,27 @@ public function testTransactionLogging() {
$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
);
}

}

0 comments on commit 2ad0f8b

Please sign in to comment.