⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

is soft deletion bloking after delete callbacks?



cesarcruzmx
  • profile picture
  • Member

Posted 24 March 2021 - 20:03 PM

Hello every one!

 

I'm using this extension for my grocerycrud implementation (GC v. 1.6.1 over CI 3.1.11): https://github.com/greysama/extension_grocery_crud

 

Everything works good except when I try to do an after delete callback.

 

My controller's method looks like this:

 

public function master()
    {
        try {
            $crud = new Extension_grocery_CRUD();
 
            $crud->gc_initialize('entregas', 'entrega');
            $crud->order_by('fecha_entrega', 'DESC');
 
            $crud->columns('recoleccion', 'codigo_entrega', 'fecha_entrega', 'id_vehiculo', 'id_chofer');
 
            [ more staff here ]
            
            $crud->callback_after_delete( array($this,'after_delete_entrega') );
 
            $output = $crud->render();
 
            $this->_theme_output($output);

 

        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

 

the callback within the same controller is:

 

    public function after_delete_entrega($primary_key){
        log_message('DEBUG','HOLA3 HOLA3 HOLA3 HOLA3 HOLA3 '); // NEVER LOGGED
        $this->db->query("DELETE FROM entregas_detalle WHERE id_entrega=$primary_key");
 
    }

 

 

When it reaches callback_after_delete() in the extended library it jumps from 1 ==> to 2 ==> without passing or calling extended_callback_after_delete() therefore it never reaches after_delete_entrega() in the controller.

 

this is in the extention:

 

    public function callback_after_delete($callback = null,$override_all=0){
        if(!$override_all){
            $this->callback_after_delete_ext[] = $callback;
            if($this->callback_after_delete == null){
1 ==>                $this->callback_after_delete = array($this,'extended_callback_after_delete') ;
            }
        }else{
            parent::callback_after_delete($callback);
        }
 

2 ==>        return $this;

    }
 
    protected function extended_callback_after_delete($primary_key){
        $continue=1;
        foreach ($this->callback_after_delete_ext as $key => $callback) {
            if($continue){
                log_message('DEBUG','CALLBACK CALLBACK CALLBACK  CALLBACK '); // NEVER LOGGED
                $continue = call_user_func($callback, $primary_key);
            }
        }
 
        return $continue;
    }

 

My guess is that when I (soft) delete a record and the function soft_delete_me() is called (by the way the soft deletion works perfect!), it is not returning true or that the "delete" process was completed successfully so the after delete callback is never called

 

    public function soft_delete_me($primary_key)
    {
        $field = $this->extension_extras['soft_delete']['field'];
        $value = $this->extension_extras['soft_delete']['deleted_value'];
        return $this->_ci->db->update($this->basic_db_table, array($field => $value, 'sys_deleted_on' => date("Y-m-d H:i:s")), array($this->get_primary_key() => $primary_key));
    }
 
 
I've also tried passing 1 to run the callback as the grocerycrud library but didn't work;
 

            $crud->callback_after_delete( array($this,'after_delete_entrega') , 1 ); 

 

When I do this it never calls the callback_after_delete() function in the extension because of the extra parameter of course.

 

Am I missing something or is related whit the soft deletion process?

 

NOTE: 1 ==> and 2 ==> are just for reference and they are not part of my code.

 


cesarcruzmx
  • profile picture
  • Member

Posted 31 March 2021 - 17:54 PM

Well, so far I have resolved it adding an action and a 

 

 
    public function detalle_entrega($id_entrega)    {
        try {
            $crud = new Extension_grocery_CRUD();
 
            [...]
 
            $crud->add_action('Quitar', null, null,'fa fa-remove action_remove confirm_quitar',array($this,'action_remove'));
 
            $output = $crud->render();
 
            $output->page_title = "Detalle de la entrega";
            $output->js_files[] = base_url("assets/js/embarques.js");
            
            $this->_theme_output($output);
 
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }
 
    public function action_remove($primary_key , $row){
        return site_url('embarques/ajax_quitar_op/'.$row->id_entregas_detalle );
    }
 
    public function ajax_quitar_op($id_entregas_detalle){
        header('Content-Type: application/json; charset=utf-8');
        http_response_code(200);
 
        $this->db->where('id_entregas_detalle', $id_entregas_detalle);
        $this->db->delete('entregas_detalle');
 
        if ($this->db->affected_rows > 0){
            echo json_encode(['success'=>true,'error_message'=>'']);
        }else{
            echo json_encode(['success'=>false,'error_message'=>'Error']);
        }
    }

 

 

and in the js, I request a confirmation before deleting the record and refresh the list after a succesful processing:

 

$('.ajax_list').on('click','.confirm_quitar', function(){
var delete_url = $(this).attr('href');
 
 
var this_container = $(this).closest('.flexigrid');
 
if( confirm( '¿Estás seguro que quieres quitar esta orden de producción?' ) )
{
$.ajax({
url: delete_url,
dataType: 'json',
success: function(data)
{
if(data.success)
{
this_container.find('.ajax_refresh_and_loading').trigger('click');
}
else
{
error_message(data.error_message);
 
}
}
});
}
 
return false;
});

 

 

But still wondering if the soft delete functionality is preventing to execute the after delete callback or is something I should consider in my coding.