spiral / app

Spiral Framework Skeleton HTTP Application: Queue, Console, Cycle ORM
https://spiral.dev/
MIT License
191 stars 20 forks source link

Problem with tutorial switching from `PostView` to `PostGrid` #145

Closed jtheuerkauf closed 6 months ago

jtheuerkauf commented 6 months ago

I've been following the current tutorial and reached this point: https://spiral.dev/docs/cookbook-quick-start/current/en#post-view-mapper

Implementing the PostView works, but switching to the PostGrid causes an error.

// Controller::list() :
#[Route(route: '/api/post', name: 'post.list', methods: 'GET')]
#[DataGrid(grid: DataGrid::class)]
public function list(): Select
{
    return $this->posts->findAllWithAuthor();
}

// PostGrid:
#[Prototyped(property: 'postGrid')]
final class PostGrid extends GridSchema
{
    use PrototypeTrait;

    public function __construct(
        private readonly PostView $view,
    ) {
        $this->addFilter('author', new Equals('author.id', new IntValue()));

        $this->addSorter('id', new Sorter('id'));
        $this->addSorter('author', new Sorter('author.id'));

        $this->setPaginator(new PagePaginator(10, [10, 25, 50]));
    }

    public function __invoke(Post $post): array
    {
        return $this->view->map($post);
    }
}

i get this exception, which i can't find references to online:

Error: Object of class Cycle\ORM\Select could not be converted to string in
.../demo/vendor/spiral/framework/src/Router/src/CoreHandler.php:160
Stack trace ``` #0 .../demo/vendor/spiral/framework/src/Router/src/CoreHandler.php(129): Spiral\Router\CoreHandler->wrapResponse() #1 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(97): Spiral\Router\CoreHandler->handle() #2 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Http\Pipeline::Spiral\Http\{closure}() #3 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(95): Spiral\Core\Container->runScope() #4 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(55): Spiral\Http\Pipeline->handle() #5 .../demo/vendor/spiral/framework/src/Router/src/Route.php(121): Spiral\Http\Pipeline->process() #6 .../demo/vendor/spiral/framework/src/Router/src/Router.php(94): Spiral\Router\Route->handle() #7 [internal function]: Spiral\Router\Router->Spiral\Router\{closure}() #8 .../demo/vendor/spiral/framework/src/Core/src/Internal/Invoker.php(74): ReflectionFunction->invokeArgs() #9 .../demo/vendor/spiral/framework/src/Core/src/Container.php(289): Spiral\Core\Internal\Invoker->invoke() #10 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(31): Spiral\Core\Container->invoke() #11 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Telemetry\AbstractTracer::Spiral\Telemetry\{closure}() #12 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(28): Spiral\Core\Container->runScope() #13 .../demo/vendor/spiral/framework/src/Telemetry/src/NullTracer.php(25): Spiral\Telemetry\AbstractTracer->runScope() #14 .../demo/vendor/spiral/framework/src/Router/src/Router.php(68): Spiral\Telemetry\NullTracer->trace() #15 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(97): Spiral\Router\Router->handle() #16 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Http\Pipeline::Spiral\Http\{closure}() #17 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(95): Spiral\Core\Container->runScope() #18 .../demo/vendor/spiral/framework/src/Framework/Debug/StateCollector/HttpCollector.php(21): Spiral\Http\Pipeline->handle() #19 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(72): Spiral\Debug\StateCollector\HttpCollector->process() #20 [internal function]: Spiral\Http\Pipeline->Spiral\Http\{closure}() #21 .../demo/vendor/spiral/framework/src/Core/src/Internal/Invoker.php(74): ReflectionFunction->invokeArgs() #22 .../demo/vendor/spiral/framework/src/Core/src/Container.php(289): Spiral\Core\Internal\Invoker->invoke() #23 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(31): Spiral\Core\Container->invoke() #24 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Telemetry\AbstractTracer::Spiral\Telemetry\{closure}() #25 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(28): Spiral\Core\Container->runScope() #26 .../demo/vendor/spiral/framework/src/Telemetry/src/NullTracer.php(25): Spiral\Telemetry\AbstractTracer->runScope() #27 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(69): Spiral\Telemetry\NullTracer->trace() #28 .../demo/vendor/spiral/framework/src/Http/src/Middleware/JsonPayloadMiddleware.php(39): Spiral\Http\Pipeline->handle() #29 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(72): Spiral\Http\Middleware\JsonPayloadMiddleware->process() #30 [internal function]: Spiral\Http\Pipeline->Spiral\Http\{closure}() #31 .../demo/vendor/spiral/framework/src/Core/src/Internal/Invoker.php(74): ReflectionFunction->invokeArgs() #32 .../demo/vendor/spiral/framework/src/Core/src/Container.php(289): Spiral\Core\Internal\Invoker->invoke() #33 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(31): Spiral\Core\Container->invoke() #34 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Telemetry\AbstractTracer::Spiral\Telemetry\{closure}() #35 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(28): Spiral\Core\Container->runScope() #36 .../demo/vendor/spiral/framework/src/Telemetry/src/NullTracer.php(25): Spiral\Telemetry\AbstractTracer->runScope() #37 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(69): Spiral\Telemetry\NullTracer->trace() #38 .../demo/vendor/spiral/dumper/src/Middleware/DumperMiddleware.php(27): Spiral\Http\Pipeline->handle() #39 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(72): Spiral\Debug\Middleware\DumperMiddleware->process() #40 [internal function]: Spiral\Http\Pipeline->Spiral\Http\{closure}() #41 .../demo/vendor/spiral/framework/src/Core/src/Internal/Invoker.php(74): ReflectionFunction->invokeArgs() #42 .../demo/vendor/spiral/framework/src/Core/src/Container.php(289): Spiral\Core\Internal\Invoker->invoke() #43 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(31): Spiral\Core\Container->invoke() #44 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Telemetry\AbstractTracer::Spiral\Telemetry\{closure}() #45 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(28): Spiral\Core\Container->runScope() #46 .../demo/vendor/spiral/framework/src/Telemetry/src/NullTracer.php(25): Spiral\Telemetry\AbstractTracer->runScope() #47 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(69): Spiral\Telemetry\NullTracer->trace() #48 .../demo/vendor/spiral/framework/src/Http/src/Middleware/ErrorHandlerMiddleware.php(45): Spiral\Http\Pipeline->handle() #49 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(72): Spiral\Http\Middleware\ErrorHandlerMiddleware->process() #50 [internal function]: Spiral\Http\Pipeline->Spiral\Http\{closure}() #51 .../demo/vendor/spiral/framework/src/Core/src/Internal/Invoker.php(74): ReflectionFunction->invokeArgs() #52 .../demo/vendor/spiral/framework/src/Core/src/Container.php(289): Spiral\Core\Internal\Invoker->invoke() #53 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(31): Spiral\Core\Container->invoke() #54 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Telemetry\AbstractTracer::Spiral\Telemetry\{closure}() #55 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(28): Spiral\Core\Container->runScope() #56 .../demo/vendor/spiral/framework/src/Telemetry/src/NullTracer.php(25): Spiral\Telemetry\AbstractTracer->runScope() #57 .../demo/vendor/spiral/framework/src/Http/src/Pipeline.php(69): Spiral\Telemetry\NullTracer->trace() #58 .../demo/vendor/spiral/framework/src/Http/src/Http.php(74): Spiral\Http\Pipeline->handle() #59 [internal function]: Spiral\Http\Http->Spiral\Http\{closure}() #60 .../demo/vendor/spiral/framework/src/Core/src/Internal/Invoker.php(74): ReflectionFunction->invokeArgs() #61 .../demo/vendor/spiral/framework/src/Core/src/Container.php(289): Spiral\Core\Internal\Invoker->invoke() #62 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(31): Spiral\Core\Container->invoke() #63 .../demo/vendor/spiral/framework/src/Core/src/Container.php(187): Spiral\Telemetry\AbstractTracer::Spiral\Telemetry\{closure}() #64 .../demo/vendor/spiral/framework/src/Telemetry/src/AbstractTracer.php(28): Spiral\Core\Container->runScope() #65 .../demo/vendor/spiral/framework/src/Telemetry/src/NullTracer.php(25): Spiral\Telemetry\AbstractTracer->runScope() #66 .../demo/vendor/spiral/framework/src/Http/src/Http.php(95): Spiral\Telemetry\NullTracer->trace() #67 .../demo/vendor/spiral/roadrunner-bridge/src/Http/Dispatcher.php(43): Spiral\Http\Http->handle() #68 .../demo/vendor/spiral/framework/src/Boot/src/AbstractKernel.php(287): Spiral\RoadRunnerBridge\Http\Dispatcher->serve() #69 .../demo/vendor/spiral/framework/src/Core/src/ContainerScope.php(39): Spiral\Boot\AbstractKernel::Spiral\Boot\{closure}() #70 .../demo/vendor/spiral/framework/src/Core/src/Container.php(186): Spiral\Core\ContainerScope::runScope() #71 .../demo/vendor/spiral/framework/src/Boot/src/AbstractKernel.php(283): Spiral\Core\Container->runScope() #72 .../demo/app.php(32): Spiral\Boot\AbstractKernel->serve() #73 {main} ```

With this code highlighted (Router/src/CoreHandler ~160): image

Any help?

jtheuerkauf commented 6 months ago

Attaching my composer.lock file to provide what i versions of stuff i have installed. composer.lock.json

roxblnfk commented 6 months ago

Looks like there is a mistake:

#[DataGrid(grid: DataGrid::class)] // <=== should be PostGrid::class
public function list(): Select
jtheuerkauf commented 6 months ago

Oops! i know for sure it has been correct in previous attempts, but thanks for catching that.

Okay, that is corrected, but the error is the same.

#[Route(route: '/api/post', name: 'post.list', methods: 'GET')]
#[DataGrid(grid: PostGrid::class)]
public function list(): Select
{
    return $this->posts->findAllWithAuthor();
}

i double checked the PrototypeTrait this time, too...

/*
 * ...
 * @property \App\Endpoint\Web\View\PostGrid $postGrid
 * ...
 */
trait PrototypeTrait
jtheuerkauf commented 6 months ago

Oops! i know for sure it has been correct in previous attempts, but thanks for catching that.

i did a comparison between what i wrote through the tutorial and the source code linked in the tutorial, and found some discrepancies - things that seemingly should have been set up when the project was created, because they're not mentioned in the tutorial itself.

After those adjustments, the DataGrid step is working!

http://127.0.0.1:8080/api/post?filter[author]=25

image

Thanks for the help. Closing this issue.