Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformer not working for Collection responses #30

Closed
thylo opened this issue May 8, 2014 · 5 comments
Closed

Transformer not working for Collection responses #30

thylo opened this issue May 8, 2014 · 5 comments

Comments

@thylo
Copy link

thylo commented May 8, 2014

Hi,

I could be wrong but it seems that collections responses are not transformed by my Transformer. It do work for single Model returns :

Here is my code

API::transform('User', 'UserTransformer');
Route::api(['version' => 'v1','prefix'=>'api', 'protected' => true], function()
{
    Route::get('me', function(){
        return API::user();
    });
    Route::get('users', function()
    {
        return User::all();
    });
    Route::resource('room-types', 'Admin\Controllers\Api\RoomTypesController');
});

returns for /api/me is correct (name parameter is correct) :

{
    "data": {
        "id": 3,
        "name": "Julien Moreau",
        "email": "admin@example.com",
        "activated": true
    }
}

meanwhile return for /api/users is not correct

{
    "users": [
        {
            "id": 1,
            "email": "admin@admin.com",
            "last_login": "2014-04-30 14:28:03",
            "first_name": "Admin",
            "last_name": null,
            "updated_at": "2014-04-30 14:28:03"
        },
        {
            "id": 2,
            "email": "user@user.com",
            "last_login": null,
            "first_name": null,
            "last_name": null,
            "updated_at": "2014-04-30 14:24:00"
        },
        {
            "id": 3,
            "email": "admin@example.com",
            "last_login": "2014-05-01 08:02:54",
            "first_name": "Julien",
            "last_name": "Moreau",
            "updated_at": "2014-05-01 08:02:54"
        }
    ]
}

The "testTransformingCollectionUsingTransformerClassName" test from "TransformerTest.php" seems to ensure that it works.

Other question, why is the key param for single models "data" and not "user" as it should be?

@thylo
Copy link
Author

thylo commented May 8, 2014

Regarding my last question about the key param "data". It seems to be hardcoded in League\Fractal|\Scope

Line 110:

110 public function toArray()
111 {
112   $output = array(
113     'data' => $this->runAppropriateTransformer()
114    );
115 ..

I'll file a bug report for that

EDIT: My bad, it already on tracks thephpleague/fractal#45

@jasonlewis
Copy link
Contributor

Will look into it now. Cheers. 😄

@jasonlewis
Copy link
Contributor

Fixed and tagged in v0.3.1.

@thylo
Copy link
Author

thylo commented May 8, 2014

Thanks a lot @jasonlewis 😄

@jasonlewis
Copy link
Contributor

No worries. Hopefully soon the PR on Fractal will be right to go so we can use a custom serializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants