敏捷性:如何呈现嵌入式对象?
Apigility: How to render embedded objects?
如何在 Apigility 中渲染嵌入对象?例如,如果我有一个 'user' 对象并且它组成了一个 'country' 对象,我是否应该将 'country' 对象呈现为嵌入对象?我应该怎么做?
我正在使用Zend\Stdlib\Hydrator\ArraySerializable
。我的 getArrayCopy()
方法只是 return 一个我想要公开的属性数组。数组键是 属性 名称。数组值是 属性 值。在 user->country
的情况下,该值是一个对象,而不是标量。
当我 return 来自 UserResource->fetch()
的用户对象时,它是这样呈现的:
{
"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"
}
}
}
请注意 'country' 不在 _embedded
字段中。如果它应该在 _embedded
中,我会认为 Apigility 会自动执行此操作(因为它会自动添加 _links
对象)。
作为一个相关问题,我该如何处理 return 其他相关链接,例如后退、前进等?
获得 Apigility 以呈现嵌入式资源的最简单方法是当有一个 API/resource 关联到嵌入式 object 时。对于您的示例,我的意思是您将拥有一个具有国家/地区实体的 API 资源。在这种情况下,如果您的 getArrayCopy returned 了 CountryEntity,Apigility 会自动将其呈现为嵌入式资源。
如果您的 getArrayCopy return将国家/地区作为包含代码和名称的数组,您将得到您所看到的结果。
对于另一部分,当您 return 分页器时,first、last、prev 和 next 的 rel 链接将来自 fetchAll 方法。您的 collection 已经从这里扩展,但它需要一个适配器。代码可能如下所示:
public function fetchAll($params)
{
// Return a \Zend\Db\Select object that will retrieve the
// stuff you want from the 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);
}
还有其他分页器适配器 - 一个 ArrayAdapter,它将接收一个数组,无论大小如何,然后对其进行分页,以便您只获得所需数量的结果。不利的一面是,如果您将它与数据库结果一起使用,您可能会检索和丢弃大量结果。 DbSelect 分页器将修改 $select object 以自动添加限制和顺序子句,以便您只检索所需的位。如果您使用 DbTableGateway、迭代器甚至回调,也有适配器。您当然也可以实现自己的。
希望这对您有所帮助。如果您有更具体的需求或说明,请发表评论,我会尽力而为。
我在 github 上发布了这个例子。
https://github.com/martins-smb/apigility-renderCollection-example
希望对您有所帮助。
如何在 Apigility 中渲染嵌入对象?例如,如果我有一个 'user' 对象并且它组成了一个 'country' 对象,我是否应该将 'country' 对象呈现为嵌入对象?我应该怎么做?
我正在使用Zend\Stdlib\Hydrator\ArraySerializable
。我的 getArrayCopy()
方法只是 return 一个我想要公开的属性数组。数组键是 属性 名称。数组值是 属性 值。在 user->country
的情况下,该值是一个对象,而不是标量。
当我 return 来自 UserResource->fetch()
的用户对象时,它是这样呈现的:
{
"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"
}
}
}
请注意 'country' 不在 _embedded
字段中。如果它应该在 _embedded
中,我会认为 Apigility 会自动执行此操作(因为它会自动添加 _links
对象)。
作为一个相关问题,我该如何处理 return 其他相关链接,例如后退、前进等?
获得 Apigility 以呈现嵌入式资源的最简单方法是当有一个 API/resource 关联到嵌入式 object 时。对于您的示例,我的意思是您将拥有一个具有国家/地区实体的 API 资源。在这种情况下,如果您的 getArrayCopy returned 了 CountryEntity,Apigility 会自动将其呈现为嵌入式资源。
如果您的 getArrayCopy return将国家/地区作为包含代码和名称的数组,您将得到您所看到的结果。
对于另一部分,当您 return 分页器时,first、last、prev 和 next 的 rel 链接将来自 fetchAll 方法。您的 collection 已经从这里扩展,但它需要一个适配器。代码可能如下所示:
public function fetchAll($params)
{
// Return a \Zend\Db\Select object that will retrieve the
// stuff you want from the 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);
}
还有其他分页器适配器 - 一个 ArrayAdapter,它将接收一个数组,无论大小如何,然后对其进行分页,以便您只获得所需数量的结果。不利的一面是,如果您将它与数据库结果一起使用,您可能会检索和丢弃大量结果。 DbSelect 分页器将修改 $select object 以自动添加限制和顺序子句,以便您只检索所需的位。如果您使用 DbTableGateway、迭代器甚至回调,也有适配器。您当然也可以实现自己的。
希望这对您有所帮助。如果您有更具体的需求或说明,请发表评论,我会尽力而为。
我在 github 上发布了这个例子。
https://github.com/martins-smb/apigility-renderCollection-example
希望对您有所帮助。