Skip to content

Commit 3cecdc7

Browse files
committed
Fixing code and adding test for the Between expression
1 parent 76cc182 commit 3cecdc7

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/Database/Expression/BetweenExpression.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class BetweenExpression implements ExpressionInterface {
4545
*/
4646
protected $_to;
4747

48+
/**
49+
* The data type for the from and to arguments
50+
*
51+
* @var mixed
52+
*/
53+
protected $_type;
54+
4855
/**
4956
* Constructor
5057
*
@@ -55,7 +62,7 @@ public function __construct($field, $from, $to, $type = null) {
5562
$this->_field = $field;
5663
$this->_from = $from;
5764
$this->_to = $to;
58-
$this->$type = $type;
65+
$this->_type = $type;
5966
}
6067

6168
/**

src/Database/Expression/QueryExpression.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ public function notIn($field, $values, $type = null) {
298298
return $this->add(new Comparison($field, $values, $type, 'NOT IN'));
299299
}
300300

301+
public function between($field, $from, $to, $type = null) {
302+
return $this->add(new BetweenExpression($field, $from, $to, $type));
303+
}
304+
301305
// @codingStandardsIgnoreStart
302306
/**
303307
* Returns a new QueryExpression object containing all the conditions passed

tests/TestCase/Database/QueryTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,56 @@ public function testInValueCast2() {
11221122
$this->assertCount(5, $result);
11231123
}
11241124

1125+
/**
1126+
* Tests that it is possible to use a between expression
1127+
* in a where condition
1128+
*
1129+
* @return void
1130+
*/
1131+
public function testWhereWithBetween() {
1132+
$query = new Query($this->connection);
1133+
$result = $query
1134+
->select(['id'])
1135+
->from('comments')
1136+
->where(function($exp) {
1137+
return $exp->between('id', 5, 6, 'integer');
1138+
})
1139+
->execute();
1140+
1141+
$this->assertCount(2, $result);
1142+
$first = $result->fetch('assoc');
1143+
$this->assertEquals(5, $first['id']);
1144+
1145+
$second = $result->fetch('assoc');
1146+
$this->assertEquals(6, $second['id']);
1147+
}
1148+
1149+
/**
1150+
* Tests that it is possible to use a between expression
1151+
* in a where condition with a complex data type
1152+
*
1153+
* @return void
1154+
*/
1155+
public function testWhereWithBetweenComplex() {
1156+
$query = new Query($this->connection);
1157+
$result = $query
1158+
->select(['id'])
1159+
->from('comments')
1160+
->where(function($exp) {
1161+
$from = new \DateTime('2007-03-18 10:51:00');
1162+
$to = new \DateTime('2007-03-18 10:54:00');
1163+
return $exp->between('created', $from, $to, 'datetime');
1164+
})
1165+
->execute();
1166+
1167+
$this->assertCount(2, $result);
1168+
$first = $result->fetch('assoc');
1169+
$this->assertEquals(4, $first['id']);
1170+
1171+
$second = $result->fetch('assoc');
1172+
$this->assertEquals(5, $second['id']);
1173+
}
1174+
11251175
/**
11261176
* Tests nesting query expressions both using arrays and closures
11271177
*

0 commit comments

Comments
 (0)