File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -454,7 +454,18 @@ public function assembleSelect()
454454 foreach ($ resolved as $ target ) {
455455 $ targetColumns = $ resolved [$ target ]->getArrayCopy ();
456456 if (isset ($ omitted [$ target ])) {
457- $ targetColumns = array_diff ($ targetColumns , $ omitted [$ target ]->getArrayCopy ());
457+ $ toExclude = $ omitted [$ target ]->getArrayCopy ();
458+ $ targetColumns = array_filter ($ targetColumns , function ($ column , $ alias ) use ($ toExclude ) {
459+ if (is_string ($ alias ) && isset ($ toExclude [$ alias ])) {
460+ return false ;
461+ }
462+
463+ if (is_string ($ alias )) {
464+ return ! in_array ($ alias , $ toExclude , true );
465+ }
466+
467+ return ! in_array ($ column , $ toExclude , true );
468+ }, ARRAY_FILTER_USE_BOTH );
458469 }
459470
460471 if (! empty ($ customAliases )) {
Original file line number Diff line number Diff line change 44
55use ipl \Orm \Exception \InvalidRelationException ;
66use ipl \Orm \Query ;
7+ use ipl \Orm \ResolvedExpression ;
78use ipl \Sql \Expression ;
89use ipl \Tests \Sql \TestCase ;
910
@@ -401,6 +402,32 @@ public function testWithColumnsSupportsExpressions()
401402 );
402403 }
403404
405+ public function testWithColumnsSupportsAliasedExpressions ()
406+ {
407+ $ model = new class () extends User {
408+ public function getColumns (): array
409+ {
410+ return ['username ' , 'aliased_expr ' => new Expression ('1 ' )];
411+ }
412+ };
413+
414+ $ query = (new Query ())
415+ ->setModel ($ model )
416+ ->columns ('username ' )
417+ ->withColumns ('aliased_expr ' );
418+
419+ $ this ->assertEquals (
420+ [
421+ 'user.username ' ,
422+ 'aliased_expr ' => new ResolvedExpression (
423+ new Expression ('1 ' ),
424+ $ query ->getResolver ()->requireAndResolveColumns (['aliased_expr ' ])
425+ ),
426+ ],
427+ $ query ->assembleSelect ()->getColumns ()
428+ );
429+ }
430+
404431 public function testWithColumnsHandlesCustomAliasesCorrectly ()
405432 {
406433 $ query = (new Query ())
@@ -486,6 +513,28 @@ public function testWithoutColumnsOverridesColumnsAndWithColumns()
486513 );
487514 }
488515
516+ public function testWithoutColumnsExcludesAliasedExpressions ()
517+ {
518+ $ model = new class () extends User {
519+ public function getColumns (): array
520+ {
521+ return [
522+ 'username ' ,
523+ 'aliased_expr ' => new Expression ('1 ' )
524+ ];
525+ }
526+ };
527+
528+ $ query = (new Query ())
529+ ->setModel ($ model )
530+ ->withoutColumns (['aliased_expr ' , 'id ' ]);
531+
532+ $ this ->assertSame (
533+ ['user.username ' ],
534+ $ query ->assembleSelect ()->getColumns ()
535+ );
536+ }
537+
489538 public function testWithoutColumnsDoesNotWorkWithExpressions ()
490539 {
491540 $ expression = new Expression ('1 ' );
You can’t perform that action at this time.
0 commit comments