psx/nested
Composer 安装命令:
composer require psx/nested
包简介
Build complex nested data structures from relational databases
README 文档
README
This library helps to build complex nested JSON responses based on relational tables.
Basic usage
At the core this library provides a Builder class which allows you to define a definition structure how the result
of your JSON response should look. To give you an example the following code is from our test case and should give you
a first insight how you can generate a complex JSON structure s.
<?php $connection = null; // a doctrine DBAL connection $builder = new \PSX\Nested\Builder($connection); $definition = [ 'totalResults' => $builder->doValue('SELECT COUNT(*) AS cnt FROM psx_sql_provider_news', [], $builder->fieldInteger('cnt')), 'entries' => $builder->doCollection('SELECT * FROM psx_sql_provider_news ORDER BY id DESC', [], [ 'id' => $builder->fieldInteger('id'), 'title' => $builder->fieldCallback('title', function($title){ return ucfirst($title); }), 'author' => $builder->doEntity('SELECT * FROM psx_sql_provider_author WHERE id = ?', [new Reference('author_id')], [ 'id' => $builder->fieldFormat('id', 'urn:profile:%s'), 'name' => 'name', 'uri' => 'uri', ]), 'tags' => $builder->doColumn('SELECT title FROM psx_sql_provider_news', [], 'title'), 'date' => $builder->fieldDateTime('create_date'), ]) ]; $result = $builder->build($definition); echo \json_encode($result);
This would result in the following JSON payload
{
"totalResults": 2,
"entries": [
{
"id": 2,
"title": "Bar",
"author": {
"id": "urn:profile:1",
"name": "Foo Bar",
"uri": "https:\/\/phpsx.org"
},
"tags": [
"foo",
"bar"
],
"date": "2016-03-01T00:00:00Z"
},
{
"id": 1,
"title": "Foo",
"author": {
"id": "urn:profile:1",
"name": "Foo Bar",
"uri": "https:\/\/phpsx.org"
},
"tags": [
"foo",
"bar"
],
"date": "2016-03-01T00:00:00Z"
}
]
}
JSON Definition
It is also possible to declare the definition in a JSON notation
{
"totalEntries": {
"$value": "SELECT COUNT(*) AS cnt FROM psx_sql_provider_news",
"$definition": {
"$key": "cnt",
"$field": "integer"
}
},
"entries": {
"$collection": "SELECT id, author_id, title, create_date FROM psx_sql_provider_news ORDER BY id ASC LIMIT :startIndex, 8",
"$params": {
"startIndex": 0
},
"$definition": {
"id": {
"$key": "id",
"$field": "integer"
},
"title": "title",
"tags": {
"$column": "SELECT title FROM psx_sql_provider_news",
"$definition": "title"
},
"author": {
"$entity": "SELECT id, name, uri FROM psx_sql_provider_author WHERE id = :id",
"$params": {
"id": {
"$ref": "author_id"
}
},
"$definition": {
"displayName": "name",
"uri": "uri"
}
}
}
}
}
Then you can execute this JSON definition through the JSON provider
<?php $json = '{}'; // JSON from above $provider = new JsonProvider($this->connection); $result = $provider->create(\json_decode($json)); echo \json_encode($result);
psx/nested 适用场景与选型建议
psx/nested 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.93k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sql」 「json」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psx/nested 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psx/nested 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psx/nested 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
Query filtering in your frontend
A package to cast json fields, each sub-keys is castable
Database/ORM-agnostic query construction helper
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 13.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2023-04-02