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

Allow a relation to have an array of values #293

Open
goetas opened this issue May 11, 2019 · 1 comment
Open

Allow a relation to have an array of values #293

goetas opened this issue May 11, 2019 · 1 comment

Comments

@goetas
Copy link
Collaborator

goetas commented May 11, 2019

A relation link can contain multiple links.

See example case as in #290 (comment)

I think is a bad idea to give to relation providers access to object, probably can be avoided by still creating only one relation, but the allowing multiple links in it.

@egonolieux
Copy link

Replying to #290 (comment) here.

I'm certainly willing to work on this. I can't say when however as I'm quite busy at this time.

For those interested in a "decent enough" workaround for the time being:

@Hateoas\Relation(
    "books",
    href="",
    attributes={"href": "expr(service('books_to_links_mapper').mapToLinks(object.getBooks(), 'book', { 'bookId': 'id' }))"},
    exclusion=@Hateoas\Exclusion(excludeIf="expr(object.getBooks() == [])")
)

It essentially overrides the href property using attributes, on which a service call can be made to convert the books to links. The arguments of mapToLinks are the books, the route name of an individual book and the properties needed for the route parameters.

This gives the following result:

"_links": {
    "books": {
        "href": [
            "http://book_1",
            "http://book_2",
            "http://book_3"
        ]
    }
}

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

No branches or pull requests

2 participants