rompetomp / inertia-bundle

Inertia.js server-side adapter for Symfony
MIT License
151 stars 36 forks source link

Returning doctrine entities #13

Closed totaltrash closed 4 years ago

totaltrash commented 4 years ago

I think I'm missing something regarding sending a collection of entities to be rendered by Inertia. I have the following action:

    /**
     * @Route("/item/", name="item_list")
     */
    public function itemList(InertiaInterface $inertia, ItemRepository $repo)
    {
        return $inertia->render('ItemList', [
            'items' => $repo->findAll(),
        ]);
    }

Which works great when doing a hard browser refresh:

<div id="app" data-page="{&quot;component&quot;:&quot;ItemList&quot;,&quot;props&quot;:{&quot;items&quot;:[{&quot;id&quot;:1,&quot;name&quot;:&quot;Bike&quot;,&quot;price&quot;:&quot;150.00&quot;},{&quot;id&quot;:2,&quot;name&quot;:&quot;Car&quot;,&quot;price&quot;:&quot;2500.00&quot;},...}]},&quot;url&quot;:&quot;\/item\/&quot;,&quot;version&quot;:null}"></div>

But returns empty objects in the xhr response:

{"component":"ItemList","props":{"items":[{},{},{},{},{},{},{},{},{},{}]},"url":"\/item\/","version":null}

What am I missing?!

totaltrash commented 4 years ago

Looks like I need to normalize the collection first? Which makes sense, as I can then choose the serializer group to normalize to

    /**
     * @Route("/item/", name="item_list")
     */
    public function itemList(InertiaInterface $inertia, ItemRepository $repo, NormalizerInterface $normalizer)
    {
        return $inertia->render('ItemList', [
            'items' => $normalizer->normalize($repo->findAll(), null, ['groups' => 'some_group']),
        ]);
    }
rompetomp commented 4 years ago

Hi @totaltrash, sorry for getting back to you so late. I understand you solved this issue yourself?

totaltrash commented 4 years ago

Yes, thanks @rompetomp