Skip to content
Permalink
Browse files

Adding support for passing a closure to select(), it should return the

list of fields to be selected
  • Loading branch information...
lorenzo committed Mar 3, 2013
1 parent ec73b23 commit 53c92979f5963f846d9521399ecff0baf384438a
@@ -331,6 +331,10 @@ public function select($fields = [], $overwrite = false) {
return $this->_parts['select'];
}
if (is_callable($fields)) {
$fields = $fields($this);
}
if (!is_array($fields)) {
$fields = [$fields];
}
@@ -134,6 +134,20 @@ public function testSelectFieldsOnly() {
$this->assertEquals([3, 6], $result->fetch());
}
/**
* Tests that it is possible to pass a closure as fields in select()
*
* @return void
*/
public function testSelectClosure() {
$query = new Query($this->connection);
$result = $query->select(function($q) use ($query) {

This comment has been minimized.

Copy link
@markstory

markstory Mar 7, 2013

Member

How is this working? The Closure never got bound to the test case.

This comment has been minimized.

Copy link
@lorenzo

lorenzo Mar 8, 2013

Author Member

Since PHP 5.4 you can use $this inside closures. The scope is always the class where the closure was declared in.

This comment has been minimized.

Copy link
@markstory

markstory Mar 8, 2013

Member

Good to know :D

$this->assertSame($query, $q);
return ['1 + 2', '1 + 5'];
})->execute();
$this->assertEquals([3, 6], $result->fetch());
}
/**
* Tests it is possible to select fields from tables with no conditions
*

0 comments on commit 53c9297

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