【问题标题】:silverstripe RestfulServer Module - get related records with all attributessilverstripe RestfulServer 模块 - 获取所有属性的相关记录
【发布时间】:2013-06-04 12:41:16
【问题描述】:

使用 Restful 服务器模块我做这个操作:

GET /api/v1/(ClassName)?(Field)=(Val)&(Field)=(Val) - 搜索匹配的数据库记录

api 应该像这样获取与某个产品相关的所有图像的 url: http://domain.com/api/v1/Product?ArticleID=myID

很遗憾,返回的 XML 没有显示 ProductImages 的完整信息。只是属性href和id。 这里是xml:

<?xml version="1.0" encoding="UTF-8"?>
<ArrayList totalSize="1">
<Product href="http://domain.com/api/v1/Product/9.xml">
<ArticleID>myID</ArticleID>
<ID>9</ID>
<ProductImages linktype="has_many" href="http://domain.com/api/v1/Product/9/ProductImages.xml">
<ProductImage href="http://domain.com/api/v1/ProductImage/265.xml" id="265"/>
<ProductImage href="http://domain.com/api/v1/ProductImage/268.xml" id="268"/>
<ProductImage href="http://domain.com/api/v1/ProductImage/271.xml" id="271"/>
</ProductImages>
</Product>
</ArrayList>

ProductImage 实际上有更多的属性没有显示在 xml 中。如何使它们可见?我可以在执行时看到所有 ProductImage 属性,例如这: http://domain.com/api/v1/Product/9/ProductImages

我使用 curl 加载网址。 在最坏的情况下,我认为我必须从 XML 中获取 ProductImage url(例如http://domain.com/api/v1/ProductImage/271.xml)并进行多次调用。

感谢任何帮助, 弗洛里安

【问题讨论】:

  • status: 我现在正在打多个电话。但这似乎是一个缓慢的解决方案:(

标签: rest content-management-system silverstripe


【解决方案1】:

您需要在RestfulServer 中增加relationDepth。您可以通过传入relationdepth 查询字符串参数来做到这一点。

顺便说一句,它这样工作的原因是通过不必实例化所有图像对象来保持服务器上的低负载。这也意味着带宽也略低。

【讨论】:

  • 嗨!关系深度没有帮助。我认为需要展示更深层次的关系。在我的例子中 ProductImages 已经出现了,但仍然只有 id 和 href 属性。还有更多。
  • 目前我可以通过向产品添加公共 getter 函数来帮助自己。如此处所述:silverstripe.org/dataobjectmanager-module-forum/show/12526 例如,我添加了一个公共 getter,它返回一个逗号分隔的字符串,其中包含所有相关的 ProductImage url。工作正常。
猜你喜欢
  • 1970-01-01
  • 2013-12-22
  • 2019-02-24
  • 2015-09-01
  • 2015-06-18
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
相关资源
最近更新 更多