Delineate JSON recursion level in a Doctrine entity

1

Good day.

I am new to the development of PHP with Symfony (Doctrine and FosRest) and Angular 2.

The project that we are developing works through a API Rest , to which we make calls of the methods that the protocol HTTP provides us, all the response that generates the API are in JSON ,

All the results that the API throws at us in JSON, refer to the entities that we have mapped to the DB with Doctrine .

The JSON that generates the API does it through FosRest , here my configuration:

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

The configuration that I have with doctrine is the following:

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 JSON that generates the API is correct, here is an example:

"lista": [{
    "id": 1,
    "username": "admin",
    "email": "[email protected]",
    "enabled": true,
    "last_login": "2016-07-04T16:31:07-0500",
    "nombre": "admin",
    "primer_apellido": "admin",
    "segundo_apellido": "admin",
    "id_programa_docente": {
        "id": 1,
        "abreviatura": "LRI",
        "nomenclatura": "LRI",
        "descripcion": "Licenciatura en Relaciones Internacionales",
        "consecutivo": "1",
        "clave_dgp": "3453454355",
        "mes_anio_primer_periodo": "2016-06-09T10:30:00-0500",
        "campus": "HARVARD",
        "modalidad": "Escolarizado",
        "numero_registro_sege": "863676476",
        "fecha_registro_pnpc": "2016-06-09T10:30:00-0500",
        "numero_referencia_pnpc": "0976555",
        "requiere_pago": true,
        "color_rgb": "253545454",
        "ultima_actualizacion": "2016-06-17T12:51:28-0500",
        "id_nivel_estudios": {
            "id": 1,
            "clave": "LIC",
            "descripcion": "Licenciatura",
            "ultima_actualizacion": "2016-06-11T10:58:44-0500",
            "id_estatus": {
                "id": 1008,
                "valor": "Inactivo",
                "activo": true,
                "id_catalogo": {
                    "id": 1004,
                    "valor": "CatalogosEstatus",
                    "activo": true
                }
            }
        },
        "id_archivo_credencial_frontal": {
            "id": 193,
            "nombre": "FRENTE-LRI.jpg",
            "extencion": "jpeg",
            "mime_type": "image/jpeg",
            "md5": "e5643b567aa6e134ae1abcc23fdb545e",
            "is_perfil": false
        },
        "id_archivo_credencial_reversa": {
            "id": 194,
            "nombre": "REVES-LRI.jpg",
            "extencion": "jpeg",
            "mime_type": "image/jpeg",
            "md5": "d490d35f448d3fcb849cc708f7291bc5",
            "is_perfil": false
        },
        "id_estatus": {
            "id": 1007,
            "valor": "Activo",
            "activo": true,
            "id_catalogo": {
                "id": 1004,
                "valor": "CatalogosEstatus",
                "activo": true
            }
        }
    },
    "ldap": true,
    "rolesdb": [{
        "id": 8,
        "id_rol": {
            "id": 9,
            "nombre": "ADMINISTRADOR",
            "descripcion": "Rol Administrador",
            "activo": true
        }
    }, {
        "id": 9,
        "id_rol": {
            "id": 10,
            "nombre": "SHOWCASE",
            "descripcion": "Rol showcase",
            "activo": true
        }
    }, {
        "id": 10,
        "id_rol": {
            "id": 11,
            "nombre": "DESARROLLADOR",
            "descripcion": "Rol desarrollador",
            "activo": true
        }
    }]
}]

The problem I have is when an entity has multiple relationships, as you can see in the example JSON, when parsing the entity is adding all its relationships ( each key that starts with id _ it's a mapped relationship with Doctrine) and since our BD has grown considerably, there are times when a JSON has too much information that is hard to read from Angular's side.

Is there any way to delimit (optimize) up to what level the parsing (JSON) of the relationships in an entity is done?

    
asked by Green - 4 05.07.2016 в 02:23
source

0 answers