@@ -407,6 +407,41 @@ public function test_local_scope(): void
407407 $ this ->assertSame ('Alice ' , $ active ->first ()->name );
408408 }
409409
410+ /**
411+ * Regression: chaining a second local scope after the first one used
412+ * to fail with "Call to undefined method Foxdb\Query\Builder::adult()"
413+ * because the first scope call returned a raw Query\Builder instead of
414+ * a model-aware ModelBuilder, so nothing could resolve scopeAdult()
415+ * as a scope anymore.
416+ */
417+ public function test_chained_local_scopes (): void
418+ {
419+ TestUser::create (['name ' => 'Alice ' , 'email ' => 'a@test.com ' , 'age ' => 25 , 'is_active ' => 1 ]); // active adult
420+ TestUser::create (['name ' => 'Bob ' , 'email ' => 'b@test.com ' , 'age ' => 15 , 'is_active ' => 1 ]); // active minor
421+ TestUser::create (['name ' => 'Carol ' , 'email ' => 'c@test.com ' , 'age ' => 30 , 'is_active ' => 0 ]); // inactive adult
422+
423+ $ result = TestUser::active ()->adult ()->get ();
424+
425+ $ this ->assertCount (1 , $ result );
426+ $ this ->assertSame ('Alice ' , $ result ->first ()->name );
427+ }
428+
429+ /**
430+ * Regression: a scope must also stay chainable with ordinary Builder
431+ * methods that come after it (where, orderBy, ...), not just other
432+ * scopes.
433+ */
434+ public function test_local_scope_chained_with_builder_methods (): void
435+ {
436+ TestUser::create (['name ' => 'Alice ' , 'email ' => 'a@test.com ' , 'age ' => 25 , 'is_active ' => 1 ]);
437+ TestUser::create (['name ' => 'Bob ' , 'email ' => 'b@test.com ' , 'age ' => 30 , 'is_active ' => 1 ]);
438+
439+ $ result = TestUser::active ()->where ('age ' , 30 )->orderByDesc ('age ' )->get ();
440+
441+ $ this ->assertCount (1 , $ result );
442+ $ this ->assertSame ('Bob ' , $ result ->first ()->name );
443+ }
444+
410445 // -----------------------------------------------------------------------
411446 // Pagination
412447 // -----------------------------------------------------------------------
@@ -488,4 +523,9 @@ public function scopeActive(Builder $q): Builder
488523 {
489524 return $ q ->where ('is_active ' , 1 );
490525 }
526+
527+ public function scopeAdult (Builder $ q ): Builder
528+ {
529+ return $ q ->where ('age ' , '>= ' , 18 );
530+ }
491531}
0 commit comments