rtxlabs / datatransformation-bundle
提供从实体生成复杂JSON输出的映射器
1.0.0
2013-09-13 05:22 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-14 14:26:21 UTC
README
这是一个可以帮助构建REST服务的Symfony2 Bundle。
示例
提供一个返回与公司相关联的联系人列表的REST服务。服务需要提供类似以下内容
[{ "id": 1181, "address": null, "email": "max.mustermann@dtb.com", "firstname": "Max", "lastname": "Mustermann", "company": 7 } , { "id" : 1177, "address" : null, "email" : "uwe.klawitter@dtb.com", "firstname" : "Uwe", "lastname" : "Klawitter", "company": 298 }]
为了生成此内容,需要从存储库加载一些Doctrine实体并将它们转换为JSON。对实体执行json_encode()不会起作用,因为实体包含对公司的代理对象。为了解决这个问题,可以使用DoctrineBinder
$result = $this->container->get('doctrinebinder')->bind($models)->execute(); // finally the result has to be converted into json to return it as an response $json = Dencoder::decode($result);