File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -43,8 +43,15 @@ class Select extends Statement {
43
43
* @return $this
44
44
*/
45
45
public function field ($ expression , $ alias = null ) {
46
- if (is_object ($ expression )) {
47
- $ expression = (string )$ expression ;
46
+ if ($ alias !== null ) {
47
+ $ expression = (string ) $ expression ;
48
+ $ expression = trim ($ expression );
49
+ $ expression = rtrim ($ expression , '; ' );
50
+ $ expression = trim ($ expression );
51
+ $ lines = explode ("\n" , $ expression );
52
+ $ lines = array_map (function ($ line ) { return "\t\t{$ line }" ; }, $ lines );
53
+ $ expression = join ("\n" , $ lines );
54
+ $ expression = sprintf ("( \n%s \n\t) " , $ expression );
48
55
}
49
56
if ($ alias === null ) {
50
57
$ this ->fields [] = $ expression ;
Original file line number Diff line number Diff line change @@ -157,4 +157,22 @@ public function testCount() {
157
157
158
158
$ this ->assertEquals ("SELECT \n\tCOUNT(*) \nFROM \n\ttest1 t1 \nINNER JOIN \n\ttest2 t2 ON t1.id=t2.id \nWHERE \n\t(t1.id > 10) \n; \n" , $ query );
159
159
}
160
+
161
+ public function testSubselectAsField () {
162
+ $ query = TestSelect::create ()
163
+ ->field ('COUNT(*) ' )
164
+ ->from ('t1 ' , 'test1 ' )
165
+ ->joinInner ('t2 ' , 'test2 ' , 't1.id=t2.id ' )
166
+ ->where ('t1.id > 10 ' )
167
+ ->asString ();
168
+
169
+ $ query = TestSelect::create ()
170
+ ->field ($ query , 'testfield ' )
171
+ ->from ('t1 ' , 'test1 ' )
172
+ ->joinInner ('t2 ' , 'test2 ' , 't1.id=t2.id ' )
173
+ ->where ('t1.id > 10 ' )
174
+ ->asString ();
175
+
176
+ $ this ->assertEquals ("SELECT \n\tCOUNT(*) \nFROM \n\ttest1 t1 \nINNER JOIN \n\ttest2 t2 ON t1.id=t2.id \nWHERE \n\t(t1.id > 10) \n; \n" , $ query );
177
+ }
160
178
}
You can’t perform that action at this time.
0 commit comments