Search for data in a table, from a CONTROLLER, and place result into a Variable for later display


#1

Dear Saviours!

I am new to Cakephp. I am currently using Cakephp2 and I have the following issue;

THE STORY

In a Controller called GSTickets I have the code that prints PDF receipts.
This receipts lack a piece of data.

The data wanted, is a value that I must search from within a table called TIME

There are 2 tables related to find this value:
TIME
TICKET_BOOKING
Ticket_booking stores in each record a SERVICE_ID which gets writen with the value of TIME.id once we save the ticket.
The SQL query that would give me this value for a given Ticekt_booking_id = '10’
would be this:

SELECT time.suplemento FROM time,tickets_booking WHERE tickets_booking.ticket_id = ‘10’ AND tickets_booking.service_id=time.id;

THE PROBLEM

the guys that programmed this are not available, and I have to do the job.
this controller has 3,000 lines and I do not know how to implement this change, and I tried hard.
I get lost in variables and concepts and ways of quering the data base to pass the value to a variable to be able to write it in the pdf tickets.

I do not seem to integrate how to do this simple query in the cakephp 2 ways …

I am attching the controller piece of code that does this

Thank you in advance!
Jordi.
private function __pdfTicketBookingInquilinios($ticket_id){

    $conditions=array(
        'TicketInquilino.ticket_id' =>$ticket_id
    );
    
    $inquilinos=$this->_controller->TicketInquilino->find('all',
        array(
            'joins'=>array(
                
                array(
                        'table'     => 'inquilino', 
                        'alias'     => 'Inquilino', 
                        'type'      => 'INNER', 
                        'conditions'=> array('Inquilino.id = TicketInquilino.inquilino_id')
                ),
                array(
                        'table'     => 'tickets',   
                        'alias'     => 'Ticket',    
                        'type'      => 'INNER', 
                        'conditions'=> array('Ticket.id = TicketInquilino.ticket_id')
                ),
                array(
                        'table'     => 'tickets_facturacion',   
                        'alias'     => 'TicketFacturacion', 
                        'type'      => 'INNER', 
                        'conditions'=> array('Ticket.id = TicketFacturacion.ticket_id')
                ),
                array(
                        'table'     => 'tickets_clocks', 
                        'alias'     => 'TicketClock',  
                        'type'      => 'INNER', 
                        'conditions'=> array('Ticket.id = TicketClock.ticket_id')
                )
                
            ),
            'conditions'    => $conditions ,
            'fields'        => array('Ticket.*','Inquilino.*','TicketFacturacion.*','TicketClock.*'),
            'group'         => array('TicketInquilino.id'),
        )
    );
    
    foreach($inquilinos as $row){
        // añado esto  pero no funciona --- > $suplemento ==  $row['Time']['suplemento'];
        $this->__pdfCabecera($ticket_id);
        $this->__pdfRoom($ticket_id);
        
        $this->_pdf->Write(0, " \n");
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        $this->_pdf->Write(0, 'Detalle', '', 0, 'L', true, 0, false, false, 0);
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        $this->_pdf->Write(0, " \n");
        $style = array('width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0));
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        // get current vertical position
        $y = $this->_pdf->getY();
        $this->_pdf->Line(5, $y, $this->_pdf_width-5, $y, $style);
        
        $html ='<br/>
            <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tr>
                <th width="33%">Inquilino</th>
                <th width="33%">Tiempo</th>
                <th width="33%">Puntos</th>
            </tr>';
        $html .='<tr>
                    <td>' .$row['Inquilino']['apodo']. '</td>
                    <td>' .$row['TicketClock']['time']. '</td>
                    <td>' .$row['TicketFacturacion']['puntos_subtotal']/count($inquilinos) . '</td>
                </tr>';
    
        $html .='</table>';
        $this->_pdf->writeHTML($html, true, false, true, false, '');
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        $this->_pdf->Write(0, 'Total : '.$row['TicketFacturacion']['puntos_subtotal']/count($inquilinos) .'','', 0, 'L', true, 0, false, false, 0);
        $this->__pdfCopia($row['Inquilino']['apodo']);
        //$sup =  $this->request->data['Time']['service_id'];
                   
        $this->__pdfCopia('hola');
    }
    
}