简体   繁体   中英

Doctrine2 complex query for statistics

First, I'm using Doctrine2 with Symfony2 using a MySQL database. I have the following entity, it has relations with some others as you can see.

Pge\IncidenciasBundle\Entity\Incidencia:
    type: entity
    table: incidencia
    repositoryClass: Pge\IncidenciasBundle\Entity\IncidenciaRepository
    id:
      id:
        type: integer
        generator:
          strategy: IDENTITY
    fields:
        fechaInicio:
            type: date
            nullable: false
            column: fecha_inicio
        fechaFinal:
            type: date
            nullable: true
            column: fecha_final
        horaInicio:
            type: time
            nullable: true
            column: hora_inicio
        horaInicioIndisp:
            type: time
            nullable: true
            column: hora_inicio_indisp
        horaFinal:
            type: time
            nullable: true
            column: hora_final
        horaFinalIndisp:
            type: time
            nullable: true
            column: hora_final_indisp
        causa:
            type: text
            nullable: true
        resumen:
            type: string
            length: 250
            fixed: false
            nullable: true
        descripcion:
            type: text
            nullable: true
        solucion:
            type: text
            nullable: true
        remedy:
            type: string
            length: 250
            fixed: false
            nullable: true
        accionesRealizadas:
            type: text
            nullable: true
            column: acciones_realizadas          
    manyToOne:
        prioridad:
          targetEntity: Prioridad
        estado:
          targetEntity: Estado
        indisponibilidad:
          targetEntity: Indisponibilidad
        cliente:
          targetEntity: Cliente
          inversedBy: incidencia
        servicio:
          targetEntity: Servicio
    lifecycleCallbacks: {  }

I'm trying to make a query where I retrieve data for given date, with this output (the image is a handmade table in html)

在此处输入图片说明

So, for each Cliente I need to print a table, with its Servicio in each row, and a counter for each Prioridad for the found Incidencia so, if there are 3 Incidencia with Prioridad = P0 , 1 Prioridad = P1 and 2 Prioridad = P3 for Servicio = KOSMOS and Cliente = Someone , the table will output this

Someone
                SERVICIO    P0  P1  P2  P3
                KOSMOS      3   1   0   2

It is just as that easy, but my problem is that I can't find the way to generate this query... I've tried to create multiple arrays with some foreachs and stuff for some querys, creating a Cliente separated array, and for each date given, an array of services, etc but I couldn't find the way to count the Prioridad and it is obviously not a good practice because there are about 8 foreach, querys inside foreach, etc... So not good, but I'm really lost...

I think that there must be an easy way to do this, but not on my knowledge...

I obviously can't test this query so you might need to play around with it but basically we are using COUNT with multiple groupBy clauses. Also I've just typed this in the SO reply box so excuse any typos.

$queryBuilder->select('incidencia, COUNT(incidencia.id) AS incidencia_count, prioridad, cliente, servicio')
    ->from('Pge\IncidenciasBundle\Entity\Incidencia', 'incidencia')
    ->leftJoin('incidencia.prioridad', 'prioridad', 'WITH', 'prioridad = incidencia.prioridad')
    ->leftJoin('incidencia.cliente', 'cliente', 'WITH', 'cliente = incidencia.cliente')
    ->leftJoin('incidencia.servicio', 'servicio', 'WITH', 'servicio = incidencia.servicio')
    ->groupBy('prioridad.id, cliente.id, servicio.id');

(you can convert to DQL if you want, just find it easier to use QB with longer/more complex queries)

I'm not sure what field you are using for date, but just add this on as a where clause on the query.

The result you'll end up with is a number of rows with the number of incidencia for each prioridad but also for each cliente and servicio.

eg (simplified)

prioridad | cliente | servicio | incidencia_count
    p0    | someone |  kosmos  |        3
    p1    | someone |  kosmos  |        1
    p2    | someone |  kosmos  |        0
    p3    | someone |  kosmos  |        2

etc.

Just loop over this data to build your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM