I'm new to web application development with PHP, Symfony (Bundles) and Angular
we develop a web application, works over API rest building on symfony and we make request through http protocol, every response is coded in JSON.
Every JSON correspond to doctrine entity.
We use FosRest to parse entity to JSON, here my config:
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 config
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
The generated JSON API is correct, here is an example
{
"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
}
}]}
The problem I have is when an entity has multiple relationships , as you can see in the JSON example , when parsing the entity being added all your relationships (each key that starts with id_ is a mapped relationship with Doctrine) and as our BD has grown considerably , there are times when a JSON has too much information that is hard to read from Angular side .
Is there a way to define (optimize ) until the parser level (JSON ) relations is made in an entity ?
or not include each join of the entity?
regards!!
with @MaxDepth you can steer how deep you want to serialize your hierarchy and with @Groups you can manage which fields should be serialized.
Both can be set in your Serialization Context and are explained in more detail in the FOS Rest Documentation
By that you should be able to achieve what you're looking for.