thephpleague / fractal

Output complex, flexible, AJAX/RESTful data structures.
fractal.thephpleague.com
MIT License
3.53k stars 351 forks source link

Change serializer in Include - Remove data aggregator #299

Open miguelsmuller opened 8 years ago

miguelsmuller commented 8 years ago

I need to remove the item [date] when I do include, but not getting to change the serializer.

{
  "data": [
    {
      "id": 1,
      "name": "Unit 8",
      "roomType": {
        "data": {
          "id": 2,
          "description": "Economic",
        }
      }
    },
    {
      "id": 2,
      "name": "Unit 9",
      "roomType": {
        "data": {
          "id": 2,
          "description": "Economic",
        }
      }
    }
  ]
}

This is the function that does include roomType

public function includeRoomType(Room $room)
    {
        $roomType = $room->roomType;

        return $this->item($roomType, new RoomTypeTransformer);
    }
krizzdev commented 8 years ago

You mean "data" key? Answered here: https://github.com/thephpleague/fractal/issues/142

Add the ArraySerializer to your manager: $this->fractalManager->setSerializer(new ArraySerializer());

For Include collections just create your own serializer, which inherits from the ArraySerializer like jasonlewis described.

class MyArraySerializer extends League\Fractal\Serializer\ArraySerializer { public function collection($resourceKey, array $data) { return $data; } }

furey commented 8 years ago

I think (?) @miguelsmuller means he wants to keep the outer "data" key (for example, when paginating a collection) but wants to remove any include "data" keys (for example, while also returning all included relation records).

If that's right – @miguelsmuller please correct me if I'm wrong – does anyone know how to accomplish this?

miguelsmuller commented 8 years ago

Yes @furey. Exactly. I need to keep the outer "data" key but remove any include "data" keys.

krizzdev commented 8 years ago

I don't have a solution for: without "getting to change the serializer." . But in your transform() method you can set the data manual:

`namespace App\Transformers;

use League\Fractal\TransformerAbstract; use App\Transformers\ItemsTransformer;

class MyTransformer extends TransformerAbstract {

protected $defaultIncludes = [
    'items'
];

public function transform(Foo $foo)
{

    return[
        'data' => [
            'id'              => (int) $foo->id,
            'date'            => $foo->date->toDateTimeString()
        ]
    ];
}

public function includeItems(Foo $foo)
{
    $items = $foo->items;

    return $this->collection($items, new ItemsTransformer);
}

}`

And for your included items you create your own Serializer:

`namespace App\Serializers;

use League\Fractal\Serializer\ArraySerializer;

class NoDataKeySerializer extends ArraySerializer { public function collection($resourceKey, array $data) { return $data; } }`

But as I said, I don't know hot to solve this, without changing the serializer:

$fractal = new Manager(); $fractal->setSerializer(new NoDataKeySerializer());

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.