postCreate query

postCreate query

KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

Hi Im trying to do a select in a 'postCreate' function
how can I do that?

Im doing something like:

->on( 'postCreate', function ( $editor, $id, $values, $row ) {
                $editor->db()
                    ->query('select', 'mytable')
                    ->where('IdUsuario', 39)
                    
                    ->exec();
            } )

but Im getting syntax error,

Answers

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    I don't see a syntax error there. Could you show me the whole PHP file and also the error statement please?

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0
    edited July 2017

    Actually what I want to do is a select email from usuario where laboratorio = "the lab row I've created"
    Then store the value in a variable and send an email.. notification

    I've read this https://editor.datatables.net/docs/1.5.1/php/source-class-DataTables.Database.html#228-250 topic and now the select is working (I think..)

    Now I want to store de value and send the email..

    <?php
    
    /*
     * Script principal de Editor
     */
    
    // DataTables PHP library
    include( "../../php/DataTables.php" );
    
    // Alias Editor
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    $db->sql( "SET NAMES 'utf8'" );
    // Libreria para captura, lectura y edicion de datos
    Editor::inst( $db, 'altaestudios', 'IdAltaEstudios' )
        ->debug(true)
    
        ->fields(
        
            Field::inst( 'altaestudios.FechaEstudio' )
                ->validator( 'Validate::dateFormat', array(
                    "format"  => Format::DATE_ISO_8601,
                    "message" => "Ingrese un formato válido de fecha yyyy-mm-dd"
                ) )
                ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
                
                Field::inst( 'altaestudios.FechaEntrega' )
                ->validator( 'Validate::dateFormat', array(
                    "format"  => Format::DATE_ISO_8601,
                    "message" => "Ingrese un formato válido de fecha yyyy-mm-dd"
                ) )
                ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
        
                // se crea el campo que se va a relacionar
                    Field::inst( 'altaestudios.IdUsuario' )
        
                // se agregan las opciones del objeto que se va a llenar dinamicamente
                        ->options( Options::inst()
                            ->table( 'usuario'  )
                            ->value( 'IdUsuario' )
                            ->label( array('nombre', 'apellido') )
                            ->render( function ( $row ) {
                                return $row['nombre'].' '.$row['apellido'].'';
                            })
                            ->where( function ($q) {
                                $q->where( 'perfil', 3 );
                            } )
                        )
                        // validador de datos
                        ->validator( 'Validate::dbValues', array(
                            "message" => "Seleccione un paciente"
                        ) ),
                    // campo de el left join
                    Field::inst( 'paciente.nombre'),
                        Field::inst('paciente.apellido'), //Join para render de apellido
                        
                // Datos del medico     
                   Field::inst( 'altaestudios.IdMedico' )
                       ->setFormatter( 'Format::ifEmpty', null )
                // se agregan las opciones del objeto que se va a llenar dinamicamente
                        ->options( Options::inst()
                            ->table( 'usuario'  )
                            ->value( 'IdUsuario' )
                            ->label( array('nombre', 'apellido') )
                            ->render( function ( $row ) {
                                return $row['nombre'].' '.$row['apellido'].'';
                            })
                            ->where( function ($q) {
                                $q->where( 'perfil', 2 );
                            } )
                        ),
                     
                        // ->validator( 'Validate::dbValues', array(
                            // "message" => "Seleccione un médico"
                        // ) ),
                    // campo de el left join
                    Field::inst( 'medico.nombre'),
                        Field::inst('medico.apellido'), //Join para render de apellido
                     
            Field::inst( 'altaestudios.IdLaboratorio')
            ->setFormatter( 'Format::ifEmpty', 15 )
                     ->options( Options::inst()
                            ->table( 'laboratorio')
                            ->value( 'IdLaboratorio' )
                            ->label( 'NombreLaboratorio')
                        ),
                       // validador de datos
                        // ->validator( 'Validate::dbValues', array(
                            // "message" => "Seleccione un laboratorio"
                        // ) ),
                    // campo de el left join
                     Field::inst( 'laboratorio.NombreLaboratorio'),
            Field::inst( 'altaestudios.activo'),
        
            Field::inst( 'altaestudios.IdTipoEstudio')
                        ->options( Options::inst()
                            ->table( 'tipoestudio'  )
                            ->value( 'IdTipoEstudio' )
                            ->label( 'ClaveEstudio'   )
                        )
                        // validador de datos
                          ->validator( 'Validate::dbValues', array(
                            "message" => "Seleccione un estudio"
                        ) ),
                    // campo de el left join
                    Field::inst('tipoestudio.ClaveEstudio'),
                
        
        
                    Field::inst( 'altaestudios.archivo' )
                      ->setFormatter( 'Format::ifEmpty', null )
                    ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/archivos/estudioNumero__ID__.__EXTN__' )
                        ->db( 'archivo', 'IdArchivo', array(
                            'NombreArchivo'    => Upload::DB_FILE_NAME,
                            'Tamano'    => Upload::DB_FILE_SIZE,
                            'web_path'    => Upload::DB_WEB_PATH,
                            'local_path' => Upload::DB_SYSTEM_PATH
                        ) 
                    )
                        ->allowedExtensions( array( 'pdf'), "Archivo no válido" )
                )
                
        )
            
        ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
                    $data=$editor->db()
                        ->query('select')
                        ->table('usuario')
                        ->get('email')
                        ->where('laboratorio', 15)
                        ->exec();
                        
                        
                } )
            
    
            ->leftJoin( 'archivo', 'altaestudios.archivo', '=', 'archivo.IdArchivo')
            ->leftJoin( 'usuario as paciente', 'paciente.IdUsuario', '=', 'altaestudios.IdUsuario' )
            ->leftJoin( 'usuario as medico', 'medico.IdUsuario', '=', 'altaestudios.IdMedico' )
            
            ->leftJoin( 'laboratorio', 'altaestudios.IdLaboratorio', '=', 'laboratorio.IdLaboratorio')
            ->leftJoin( 'tipoestudio', 'altaestudios.IdTipoEstudio', '=', 'tipoestudio.IdTipoEstudio')
    
        ->process( $_POST )
        ->json();
    
  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    Okay good - so that select appears to be working now? You could use a query('insert') if you want to write information to another table, and also PHP's own mail method to send an email.

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Just a question allan,

    how can I see the content of $data variable?

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    I have this.. but..

    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
                    $notif = $editor->db()
                        ->query('select')
                        ->table('usuario')
                        ->get('email')
                        ->where('laboratorio', 2)
                        ->exec();
                    mail('luiscorpus182@gmail.com', 'algo', json_encode($notif));
                
                } )
            
    

    Im getting an empty {}

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    $notif is a Result class instance. You would need to use $notif->fetchAll() or $notif->fetch() to get the row.

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Hi Allan,
    thank you for your time and answers
    I been searching a lot and now Im getting the correct results in query()
    the problem is when I try to send the email says "Notice: Undefined index: email"

    Here is the function

    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
                    $res= ( $notif = $editor->db()
                        ->query('select')
                        ->table('usuario')
                        ->get('email')
                        ->where('laboratorio', $values['altaestudios']['IdLaboratorio'])
                        ->exec() );
                        
                        
    //mail('luiscorpus182@gmail.com', 'prueba', json_encode($res)); // this send correct results to my email
            $notif= array();
                while ($rows = $res->fetch() ){
                mail($notif['email'], 'Nuevo estudio disponible', 'message'); // this line give me error
                } 
                    
                } )
    

    When I send the email with the result of $res wich is correct.

    [{"email":"contacto@unidadgenetica.com"},{"email":"lledezma@unidadgenetica.com"},{"email":"ghernandez@keepmoving.com.mx"}]
    
  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    $notif= array();

    Then:

    $notif['email']

    The notif array is empty, so it is correct to tell you that there is no such index.

    I think you mean $rows['email'] (note I would suggest you rename $rows to be $row to avoid confusion in the future).

    Allan

This discussion has been closed.