rest - Apigility: How to render embedded objects? -
how render embedded objects in apigility? example, if have 'user' object , composes 'country' object, should rendering 'country' object embedded object? , how should this?
i using zend\stdlib\hydrator\arrayserializable
. getarraycopy()
method returns array of properties want exposed. array keys property names. array values property values. in case of user->country
, value object, not scalar.
when return user object userresource->fetch()
, here's how rendered:
{ "id": "1", "firstname": "joe", "lastname": "bloggs", "status": "active", "email": "test@example.com", "country": { "code": "au", "name": "australia" }, "settings": "0", "_links": { "self": { "href": "http://api.mydomain.local/users/1" } } }
note 'country' not in _embedded
field. if supposed in _embedded
, have thought apigility automatically (since automatically adds _links
object).
as related issue, how go returning other rel links, such back, forward, etc?
the easiest way apigility render embedded resources when there api/resource associated embedded object. mean example you'd have api resource has country entity. in case, if getarraycopy returned the countryentity, apigility render automatically embedded resource.
if getarraycopy returning country array code , name, you'll end saw.
for other part, rel links first, last, prev , next come fetchall method when return paginator. collection extends already, needs adapter. code this:
public function fetchall($params) { // return \zend\db\select object retrieve // stuff want database $select = $this->service->fetchall($params); $entityclass = $this->getentityclass(); $entity = new $entityclass(); $hydrator = new \zend\stdlib\arrayserializable(); $prototype = new \zend\db\resultset\hydratingresultset($hydrator, $entity); $paginator = new \zend\paginator\adapter\dbselect($select, $this->sql, $prototype); $collectionclass = $this->getcollectionclass(); return new $collectionclass($paginator); }
there other paginator adapters - arrayadapter take in array of big , paginate desired number of results. downside if use database results, you'll potentially retrieving , discarding lot of results. dbselect paginator modify $select object add limit , order clause automatically retrieve bits need. there adapters if you're using dbtablegateway, iterators or callbacks. can implement own of course.
hope helps. if have more specific needs or clarification, please comment , i'll best.
Comments
Post a Comment