Hi,
I have setup a basic REST API returning results in JSON. When I get the values from the DB and return them the JSON formatter returns everything.
What would be the best way to go about stripping out those values? ie. When returning a user I don't want to return their encrypted password.
As I am using the ORM packaging, unsetting the value simply means it gets returned with a null value, I don't want it returned at all.
Thanks.
1) Put all the values you DO want to return into an array.
2) Return the array (not the user object).
If you are setting up a RESTful interface, then when you request information about a certain resource (ie., about a user), you should be using the GET method. In that method you should already be passing some parameters (i.e., like the user id). You can add parameters that specifies which database fields you want specifically, and then return just those in your response. For instance...
http://myserver.com/user/rest.json?id=23&name=true&email=true&group=true
...sent via GET method to a "get_rest" function in the Controller_User class, specifying that you wanted returned just the 'name', 'email' and 'group' fields.
In your controller, you retrieve user with id 23, then build a data array with the appropriate keys and values, and return it.
I hope this helps.
Thanks, I appreciate the input.
I ended up taking a different approach, I've extended the REST controller with a common controller for all to use.
Then I have overriden the response function. Then when a response is returned it looks at the data being returned, if an object being converted into an array implements an IFilterable interface a filter method is called on the object. This allows the object to modify itself before being returned to the client. It also knows whether it's the primary object or a relation and can therefore return different values depending on the importance of the object.
I did this so that when I add a new field to a table / object, I don't need to modify every request controller that uses that object (directly, or in a relation). Might be completely crazy, time will tell.