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 loading entities by any field. #334

Closed
wants to merge 6 commits into from

Conversation

e0ipso
Copy link
Member

@e0ipso e0ipso commented Dec 22, 2014

I've found need this several times already. I need to be able to load an entity by a unique field or property attached to that particular entity.

This is the URL schema I'm looking for:

https://www.example.org/[resource-name]/[non-standard-id-value]?loadById=[non-standard-id-name]

At the same time we need to communicate to the API consumer that the canonical resource for what RESTful is returning is different. We should do that with a Link header and a rel="canonical" in the response. That is one of the REST principles that is really useful to normalize caches –among other things–.

Real scenario

There's a Segment content type that has a unique field. We want to be able to load by that ID.

https://www.example.org/segments/asdf-asdf-asdf-asdf?loadById=unique

Returns:

HTTP/1.1 200 OK
Date: Mon, 22 Dec 2014 08:08:53 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic,p=0.02
Transfer-Encoding: chunked
Link: https://www.example.org/segments/12; rel="canonical" <----

[RESPONSE BODY]

@amitaibu
Copy link
Member

👍

I wonder if loadById is the correct term - maybe loadByFieldName?

@amitaibu
Copy link
Member

(as, we expose the term "field" regardless is the internal is a property or a configurable field)

@e0ipso
Copy link
Member Author

e0ipso commented Dec 22, 2014

Only missing tests.

@e0ipso
Copy link
Member Author

e0ipso commented Dec 22, 2014

@amitaibu this is ready to be reviewed.

@@ -97,6 +97,35 @@ articles after a certain date:
curl https://example.com/api/articles?filter[created][value]=1417591992&filter[created][operator]=">="
```

## Loading by an alternate ID.
Some times you need to load an entity by an alternate ID that is not the regular
entity ID. All that you need to do is provide the alternate ID as the regular
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entity ID. => entity ID, for example a unique ID title

@@ -86,7 +86,7 @@ public static function controllersInfo() {
// POST
\RestfulInterface::POST => 'createEntity',
),
'^(\d+,)*\d+$' => array(
'^.*$' => array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems we are loosing an important validation here, although I'm not sure how smart we want to be about it. Any ideas? (anyway this isn't a blocker)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a big deal, since there's still validation when trying to load the entity.

@amitaibu
Copy link
Member

Looks promising :) Some comments added.

@e0ipso
Copy link
Member Author

e0ipso commented Dec 23, 2014

@amitaibu feedback implemented.

@amitaibu
Copy link
Member

go ahead - merge it :)

@e0ipso
Copy link
Member Author

e0ipso commented Dec 23, 2014

w00t! My first PR for a work related project :-)

@amitaibu
Copy link
Member

mazal tov dude :)

e0ipso pushed a commit to e0ipso/restful that referenced this pull request Dec 23, 2014
@e0ipso e0ipso closed this Dec 23, 2014
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

Successfully merging this pull request may close these issues.

2 participants