mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Adding support for virtual fields in conditions array
This commit is contained in:
parent
cf359a38b3
commit
d06ff5d10a
3 changed files with 32 additions and 2 deletions
|
@ -2103,6 +2103,11 @@ class DboSource extends DataSource {
|
|||
}
|
||||
}
|
||||
|
||||
$virtual = false;
|
||||
if (!empty($model->virtualFields[$key])) {
|
||||
$key = $model->virtualFields[$key];
|
||||
$virtual = true;
|
||||
}
|
||||
|
||||
$type = (is_object($model) ? $model->getColumnType($key) : null);
|
||||
|
||||
|
@ -2117,7 +2122,7 @@ class DboSource extends DataSource {
|
|||
|
||||
$value = $this->value($value, $type);
|
||||
|
||||
if ($key !== '?') {
|
||||
if (!$virtual && $key !== '?') {
|
||||
$isKey = (strpos($key, '(') !== false || strpos($key, ')') !== false);
|
||||
$key = $isKey ? $this->__quoteFields($key) : $this->name($key);
|
||||
}
|
||||
|
|
|
@ -1247,7 +1247,6 @@ class ControllerTest extends CakeTestCase {
|
|||
function testPaginateOrderVirtualField() {
|
||||
$Controller =& new Controller();
|
||||
$Controller->uses = array('ControllerPost', 'ControllerComment');
|
||||
$Controller->passedArgs[] = '1';
|
||||
$Controller->params['url'] = array();
|
||||
$Controller->constructClasses();
|
||||
$Controller->ControllerPost->virtualFields = array(
|
||||
|
|
|
@ -62,6 +62,32 @@ class ModelReadTest extends BaseModelTest {
|
|||
$result = $Post->field('two');
|
||||
$this->assertEqual($result,2);
|
||||
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('two' => 2),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('two <' => 3),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('NOT' => array('two >' => 3)),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$dbo =& $Post->getDataSource();
|
||||
$Post->virtualFields = array('other_field' => $dbo->name('Post.id') . ' + 1');
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('other_field' => 3),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['id'],2);
|
||||
|
||||
ClassRegistry::flush();
|
||||
$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model');
|
||||
$Writing->virtualFields = array('two' => "1 + 1");
|
||||
|
|
Loading…
Reference in a new issue