Skip to content

Commit

Permalink
Relaxing usage for Query::bind() and adding unit test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 10, 2013
1 parent 6cdf9ce commit ed49d00
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Database/Query.php
Expand Up @@ -1518,7 +1518,7 @@ public function defaultTypes(array $types = null) {
* to database
* @return Query
*/
public function bind($param, $value, $type) {
public function bind($param, $value, $type = 'string') {
$this->valueBinder()->bind($param, $value, $type);
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Database/ValueBinder.php
Expand Up @@ -44,7 +44,7 @@ class ValueBinder {
* to database
* @return void
*/
public function bind($param, $value, $type) {
public function bind($param, $value, $type = 'string') {
$this->_bindings[$param] = compact('value', 'type') + [
'placeholder' => is_numeric($param) ? $param : substr($param, 1)
];
Expand Down
28 changes: 27 additions & 1 deletion lib/Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -1094,7 +1094,7 @@ public function testSelectHaving() {
$this->assertEquals($expected, $result->fetchAll('assoc'));

$result = $query->having(function($e) {
return $e->add('count(author_id) = 1 + 1');
return $e->add('count(author_id) = 1 + 1');
}, [], true)
->execute();
$expected = [['total' => 2, 'author_id' => 1]];
Expand Down Expand Up @@ -1907,6 +1907,32 @@ public function testDefaultTypes() {
$this->assertCount(6, $results, 'All 6 rows should match.');
}

/**
* Tests parameter binding
*
* @return void
*/
public function testBind() {
$query = new Query($this->connection);
$results = $query->select(['id', 'comment'])
->from('comments')
->where(['created BETWEEN :foo AND :bar'])
->bind(':foo', new \DateTime('2007-03-18 10:50:00'), 'datetime')
->bind(':bar', new \DateTime('2007-03-18 10:52:00'), 'datetime')
->execute();
$expected = [['id' => '4', 'comment' => 'Fourth Comment for First Article']];
$this->assertEquals($expected, $results->fetchAll('assoc'));

$query = new Query($this->connection);
$results = $query->select(['id', 'comment'])
->from('comments')
->where(['created BETWEEN :foo AND :bar'])
->bind(':foo', '2007-03-18 10:50:00')
->bind(':bar','2007-03-18 10:52:00')
->execute();
$this->assertEquals($expected, $results->fetchAll('assoc'));
}

/**
* Assertion for comparing a table's contents with what is in it.
*
Expand Down

0 comments on commit ed49d00

Please sign in to comment.