File tree 3 files changed +19
-13
lines changed
3 files changed +19
-13
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,14 @@ $vt1 = $db->select()
11
11
->field('a.field1')
12
12
->from('a', 'tableA');
13
13
14
- $vt2 = $db->select()
15
- ->field('a.field1')
16
- ->from('a', 'tableA');
17
-
18
14
$db->getVirtualTables()->add('virt_table1', $vt1);
19
- $db->getVirtualTables()->add('virt_table2', $vt2);
15
+
16
+ // Lazy evaluated
17
+ $db->getVirtualTables()->add('virt_table2', function () {
18
+ return $db->select()
19
+ ->field('a.field1')
20
+ ->from('a', 'tableA');
21
+ });
20
22
```
21
23
22
24
Then use it as needed:
Original file line number Diff line number Diff line change @@ -9,10 +9,10 @@ class VirtualTables {
9
9
10
10
/**
11
11
* @param string $tableName
12
- * @param Select $select
12
+ * @param Select|\Closure $select
13
13
* @return $this
14
14
*/
15
- public function add ($ tableName , Select $ select ) {
15
+ public function add ($ tableName , $ select ) {
16
16
$ this ->virtualTables [$ tableName ] = $ select ;
17
17
return $ this ;
18
18
}
@@ -31,7 +31,11 @@ public function has($tableName) {
31
31
*/
32
32
public function get ($ tableName ) {
33
33
if ($ this ->has ($ tableName )) {
34
- return $ this ->virtualTables [$ tableName ];
34
+ $ table = $ this ->virtualTables [$ tableName ];
35
+ if ($ table instanceof \Closure) {
36
+ return call_user_func ($ table );
37
+ }
38
+ return $ table ;
35
39
}
36
40
return null ;
37
41
}
Original file line number Diff line number Diff line change @@ -347,13 +347,13 @@ public function testVirtualTables() {
347
347
->field ('a.field1 ' )
348
348
->from ('a ' , 'tableA ' );
349
349
350
- $ vt2 = TestSelect::create ()
351
- ->field ('a.field1 ' )
352
- ->from ('a ' , 'tableA ' );
353
-
354
350
$ db = new TestDB ();
355
351
$ db ->getVirtualTables ()->add ('virt_table1 ' , $ vt1 );
356
- $ db ->getVirtualTables ()->add ('virt_table2 ' , $ vt2 );
352
+ $ db ->getVirtualTables ()->add ('virt_table2 ' , function () {
353
+ return TestSelect::create ()
354
+ ->field ('a.field1 ' )
355
+ ->from ('a ' , 'tableA ' );
356
+ });
357
357
358
358
$ query = TestSelect::create ($ db )
359
359
->field ('t.field1 ' )
You can’t perform that action at this time.
0 commit comments