Skip to content
Permalink
Browse files

fixes #11870 Add DboSource::expression support for Dbo VirtualFields

  • Loading branch information...
Philippe Gibert
Philippe Gibert committed Mar 23, 2018
1 parent d63c847 commit 98bc995623e3d567fc18f349bf580b07985651de
Showing with 48 additions and 2 deletions.
  1. +12 −2 lib/Cake/Model/Datasource/DboSource.php
  2. +36 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -2578,7 +2578,12 @@ protected function _constructVirtualFields(Model $Model, $alias, $fields) {
$virtual = array();
foreach ($fields as $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}";
}
return $virtual;
@@ -2887,7 +2892,12 @@ protected function _parseKey($key, $value, Model $Model = null) {
if ($Model !== null) {
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;
}
@@ -1650,6 +1650,42 @@ public function testConditionKeysToString() {
$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
*

0 comments on commit 98bc995

Please sign in to comment.
You can’t perform that action at this time.