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

This commit is contained in:
dogmatic69 2012-03-13 17:37:17 +00:00
parent c754fb2dcb
commit 2ad0f8b8db
2 changed files with 41 additions and 1 deletions

View file

@ -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++) {

View file

@ -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
);
}
}