我如何为 JSON 解析定界实体连接级别
How i can delimite entity join level for JSON parse
我是使用 PHP、Symfony(捆绑包)和 Angular
进行 Web 应用程序开发的新手
我们开发了一个 web 应用程序,在 API 上工作,其余构建在 symfony 上,我们通过 http 协议发出请求,每个响应都在 中编码JSON.
每个JSON对应学说实体.
我们使用 FosRest 将实体解析为 JSON,这里是我的配置:
fos_rest:
param_fetcher_listener: true
routing_loader:
default_format: json
include_format: json
view:
view_response_listener: 'force'
formats:
xml: false
json: true
templating_formats:
html: false
format_listener:
rules: ~
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
service:
inflector: app.util.inflector
学说配置
doctrine:
dbal:
driver: pdo_sqlsrv #pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
metadata_cache_driver: apc
query_cache_driver: apc
生成的JSONAPI是正确的,这里是一个例子
{
"id": 1,
"username": "admin",
"email": "admin@admin.com",
"enabled": true,
"last_login": "2016-07-04T16:31:07-0500",
"name": "admin",
"first_lastName": "admin",
"second_lastName": "admin",
"id_program": {
"id": 1,
"abbreviation": "LRI",
"nomenclature": "LRI",
"description": "Licenciatura en Relaciones Internacionales",
"consecutive": "1",
"cve_dgp": "3453454355",
"first_period": "2016-06-09T10:30:00-0500",
"campus": "HARVARD",
"mod": "Escolarizado",
"number": "863676476",
"register_date": "2016-06-09T10:30:00-0500",
"ref_number": "0976555",
"require_pay": true,
"colour_rgb": "253545454",
"last_modification": "2016-06-17T12:51:28-0500",
"id_level": {
"id": 1,
"cve": "LIC",
"description": "Licenciatura",
"last_modification": "2016-06-11T10:58:44-0500",
"id_status": {
"id": 1008,
"value": "Inactivo",
"active": true,
"id_cat": {
"id": 1004,
"valur": "CatalogosEstatus",
"active": true
}
}
},
"id_front_credenctial_file": {
"id": 193,
"name": "FRENTE-LRI.jpg",
"extention": "jpeg",
"mime_type": "image/jpeg",
"md5": "e5643b567aa6e134ae1abcc23fdb545e",
"is_perfil": false
},
"id_back_file_credential": {
"id": 194,
"name": "REVES-LRI.jpg",
"extention": "jpeg",
"mime_type": "image/jpeg",
"md5": "d490d35f448d3fcb849cc708f7291bc5",
"is_perfil": false
},
"id_status": {
"id": 1007,
"value": "Activo",
"active": true,
"id_cat": {
"id": 1004,
"value": "CatalogosEstatus",
"active": true
}
}
},
"ldap": true,
"rolesdb": [{
"id": 8,
"id_rol": {
"id": 9,
"name": "ADMINISTRADOR",
"description": "Rol Administrador",
"active": true
}
}, {
"id": 9,
"id_rol": {
"id": 10,
"name": "SHOWCASE",
"description": "Rol showcase",
"active": true
}
}, {
"id": 10,
"id_rol": {
"id": 11,
"name": "DESARROLLADOR",
"description": "Rol desarrollador",
"active": true
}
}]}
我遇到的问题是,当一个实体有多个关系时,正如您在 JSON 示例中看到的那样,在解析要添加的实体时,您的所有关系(以id_ 是与 Doctrine) 的映射关系,并且随着我们的 BD 显着增长,有时 JSON 有太多信息难以从 Angular 端读取。
有没有一种方法可以定义(优化)直到在实体中建立解析器级别(JSON)关系?
或者不包括实体的每个连接?
问候!!
使用@MaxDepth,您可以控制要序列化层次结构的深度,使用@Groups,您可以管理应序列化的字段。
两者都可以在您的序列化上下文中设置,并且在 FOS Rest Documentation
中有更详细的解释
这样你应该能够实现你正在寻找的东西。
我是使用 PHP、Symfony(捆绑包)和 Angular
进行 Web 应用程序开发的新手我们开发了一个 web 应用程序,在 API 上工作,其余构建在 symfony 上,我们通过 http 协议发出请求,每个响应都在 中编码JSON.
每个JSON对应学说实体.
我们使用 FosRest 将实体解析为 JSON,这里是我的配置:
fos_rest:
param_fetcher_listener: true
routing_loader:
default_format: json
include_format: json
view:
view_response_listener: 'force'
formats:
xml: false
json: true
templating_formats:
html: false
format_listener:
rules: ~
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
service:
inflector: app.util.inflector
学说配置
doctrine:
dbal:
driver: pdo_sqlsrv #pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
metadata_cache_driver: apc
query_cache_driver: apc
生成的JSONAPI是正确的,这里是一个例子
{
"id": 1,
"username": "admin",
"email": "admin@admin.com",
"enabled": true,
"last_login": "2016-07-04T16:31:07-0500",
"name": "admin",
"first_lastName": "admin",
"second_lastName": "admin",
"id_program": {
"id": 1,
"abbreviation": "LRI",
"nomenclature": "LRI",
"description": "Licenciatura en Relaciones Internacionales",
"consecutive": "1",
"cve_dgp": "3453454355",
"first_period": "2016-06-09T10:30:00-0500",
"campus": "HARVARD",
"mod": "Escolarizado",
"number": "863676476",
"register_date": "2016-06-09T10:30:00-0500",
"ref_number": "0976555",
"require_pay": true,
"colour_rgb": "253545454",
"last_modification": "2016-06-17T12:51:28-0500",
"id_level": {
"id": 1,
"cve": "LIC",
"description": "Licenciatura",
"last_modification": "2016-06-11T10:58:44-0500",
"id_status": {
"id": 1008,
"value": "Inactivo",
"active": true,
"id_cat": {
"id": 1004,
"valur": "CatalogosEstatus",
"active": true
}
}
},
"id_front_credenctial_file": {
"id": 193,
"name": "FRENTE-LRI.jpg",
"extention": "jpeg",
"mime_type": "image/jpeg",
"md5": "e5643b567aa6e134ae1abcc23fdb545e",
"is_perfil": false
},
"id_back_file_credential": {
"id": 194,
"name": "REVES-LRI.jpg",
"extention": "jpeg",
"mime_type": "image/jpeg",
"md5": "d490d35f448d3fcb849cc708f7291bc5",
"is_perfil": false
},
"id_status": {
"id": 1007,
"value": "Activo",
"active": true,
"id_cat": {
"id": 1004,
"value": "CatalogosEstatus",
"active": true
}
}
},
"ldap": true,
"rolesdb": [{
"id": 8,
"id_rol": {
"id": 9,
"name": "ADMINISTRADOR",
"description": "Rol Administrador",
"active": true
}
}, {
"id": 9,
"id_rol": {
"id": 10,
"name": "SHOWCASE",
"description": "Rol showcase",
"active": true
}
}, {
"id": 10,
"id_rol": {
"id": 11,
"name": "DESARROLLADOR",
"description": "Rol desarrollador",
"active": true
}
}]}
我遇到的问题是,当一个实体有多个关系时,正如您在 JSON 示例中看到的那样,在解析要添加的实体时,您的所有关系(以id_ 是与 Doctrine) 的映射关系,并且随着我们的 BD 显着增长,有时 JSON 有太多信息难以从 Angular 端读取。
有没有一种方法可以定义(优化)直到在实体中建立解析器级别(JSON)关系?
或者不包括实体的每个连接?
问候!!
使用@MaxDepth,您可以控制要序列化层次结构的深度,使用@Groups,您可以管理应序列化的字段。
两者都可以在您的序列化上下文中设置,并且在 FOS Rest Documentation
中有更详细的解释这样你应该能够实现你正在寻找的东西。