mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
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:
parent
c754fb2dcb
commit
2ad0f8b8db
2 changed files with 41 additions and 1 deletions
|
@ -194,6 +194,23 @@ class DboSource extends DataSource {
|
||||||
'rollback' => 'ROLLBACK'
|
'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
|
* Separator string for virtualField composition
|
||||||
*
|
*
|
||||||
|
@ -1665,7 +1682,7 @@ class DboSource extends DataSource {
|
||||||
* @see DboSource::renderStatement()
|
* @see DboSource::renderStatement()
|
||||||
*/
|
*/
|
||||||
public function buildStatement($query, $model) {
|
public function buildStatement($query, $model) {
|
||||||
$query = array_merge(array('offset' => null, 'joins' => array()), $query);
|
$query = array_merge($this->_queryDefaults, $query);
|
||||||
if (!empty($query['joins'])) {
|
if (!empty($query['joins'])) {
|
||||||
$count = count($query['joins']);
|
$count = count($query['joins']);
|
||||||
for ($i = 0; $i < $count; $i++) {
|
for ($i = 0; $i < $count; $i++) {
|
||||||
|
|
|
@ -843,4 +843,27 @@ class DboSourceTest extends CakeTestCase {
|
||||||
$log = $db->getLog();
|
$log = $db->getLog();
|
||||||
$this->assertEquals($expected, $log['log'][0]);
|
$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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue