rtxlabs/ data-transformation-bundle
dev-master
2012-11-18 04:31 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-14 15:05:56 UTC
README
Symfony2 Bundle,可帮助构建REST服务。
示例
提供返回与公司相关联的联系人列表的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。
// loading the entity manager to create the doctrine binder instance $em = $this->getDoctrine()->getEntityManager(); // create a doctrine binder, bind the models that have been loaded before and execute the binder. The execute // method will iterate over the models and return an array containing stdClass objects with all values defined // by getters. $result = DoctrineBinder::create($em)->bind($models)->execute(); // finally the result has to be converted into json to return it as an response $json = Dencoder::decode($result);