Allow expression objects in virtual fields.

Merge branch 'issue-11871' into 2.x

Refs #11871
This commit is contained in:
mark_story 2018-04-03 12:00:04 -04:00
commit ab5578dbc9
2 changed files with 48 additions and 2 deletions

View file

@ -2578,7 +2578,12 @@ class DboSource extends DataSource {
$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 @@ class DboSource extends DataSource {
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;
}

View file

@ -1650,6 +1650,42 @@ class DboSourceTest extends CakeTestCase {
$this->assertEquals($expected, $result[0]);
}
/**
* Test conditionKeysToString() with virtual field
*
* @return void
*/
public function testConditionKeysToStringVirtualFieldExpression() {
$Article = ClassRegistry::init('Article');
$Article->virtualFields = array(
'extra' => $Article->getDataSource()->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
*