rtxlabs/data-transformation-bundle

安装: 7

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

类型:symfony-bundle

dev-master 2012-11-18 04:31 UTC

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);

文档