mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
fixes #11870 Add DboSource::expression support for Dbo VirtualFields
This commit is contained in:
parent
d63c847b61
commit
98bc995623
2 changed files with 48 additions and 2 deletions
|
@ -2578,7 +2578,12 @@ class DboSource extends DataSource {
|
||||||
$virtual = array();
|
$virtual = array();
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$virtualField = $this->name($alias . $this->virtualFieldSeparator . $field);
|
$virtualField = $this->name($alias . $this->virtualFieldSeparator . $field);
|
||||||
$expression = $this->_quoteFields($Model->getVirtualField($field));
|
$virtualFieldExpression = $Model->getVirtualField($field);
|
||||||
|
if (is_object($virtualFieldExpression) && $virtualFieldExpression->type == 'expression') {
|
||||||
|
$expression = $virtualFieldExpression->value;
|
||||||
|
} else {
|
||||||
|
$expression = $this->_quoteFields($virtualFieldExpression);
|
||||||
|
}
|
||||||
$virtual[] = '(' . $expression . ") {$this->alias} {$virtualField}";
|
$virtual[] = '(' . $expression . ") {$this->alias} {$virtualField}";
|
||||||
}
|
}
|
||||||
return $virtual;
|
return $virtual;
|
||||||
|
@ -2887,7 +2892,12 @@ class DboSource extends DataSource {
|
||||||
|
|
||||||
if ($Model !== null) {
|
if ($Model !== null) {
|
||||||
if ($Model->isVirtualField($key)) {
|
if ($Model->isVirtualField($key)) {
|
||||||
$key = $this->_quoteFields($Model->getVirtualField($key));
|
$virtualField = $Model->getVirtualField($key);
|
||||||
|
if (is_object($virtualField) && $virtualField->type == 'expression') {
|
||||||
|
$key = $virtualField->value;
|
||||||
|
} else {
|
||||||
|
$key = $this->_quoteFields($virtualField);
|
||||||
|
}
|
||||||
$virtual = true;
|
$virtual = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1650,6 +1650,42 @@ class DboSourceTest extends CakeTestCase {
|
||||||
$this->assertEquals($expected, $result[0]);
|
$this->assertEquals($expected, $result[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test conditionKeysToString() with virtual field
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testConditionKeysToStringVirtualFieldExpression() {
|
||||||
|
$Article = ClassRegistry::init('Article');
|
||||||
|
$Article->virtualFields = array(
|
||||||
|
'extra' => DboSource::expression('something virtual')
|
||||||
|
);
|
||||||
|
$conn = $this->getMock('MockPDO', array('quote'));
|
||||||
|
$db = new DboTestSource();
|
||||||
|
$db->setConnection($conn);
|
||||||
|
|
||||||
|
$conn->expects($this->at(0))
|
||||||
|
->method('quote')
|
||||||
|
->will($this->returnValue('just text'));
|
||||||
|
|
||||||
|
$conditions = array('Article.extra' => 'just text');
|
||||||
|
$result = $db->conditionKeysToString($conditions, true, $Article);
|
||||||
|
$expected = "(" . $Article->virtualFields['extra']->value . ") = just text";
|
||||||
|
$this->assertEquals($expected, $result[0]);
|
||||||
|
|
||||||
|
$conn->expects($this->at(0))
|
||||||
|
->method('quote')
|
||||||
|
->will($this->returnValue('just text'));
|
||||||
|
$conn->expects($this->at(1))
|
||||||
|
->method('quote')
|
||||||
|
->will($this->returnValue('other text'));
|
||||||
|
|
||||||
|
$conditions = array('Article.extra' => array('just text', 'other text'));
|
||||||
|
$result = $db->conditionKeysToString($conditions, true, $Article);
|
||||||
|
$expected = "(" . $Article->virtualFields['extra']->value . ") IN (just text, other text)";
|
||||||
|
$this->assertEquals($expected, $result[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test conditionKeysToString() with virtual field
|
* Test conditionKeysToString() with virtual field
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue