⚠ 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

Callback_insert does not work



fbn
  • profile picture
  • Member

Posted 25 March 2019 - 21:53 PM

Hello everybody,

I do not know why my callback_insert does not work. It does not seem to get called. My controller:

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class utilizadores extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function _render_output($output = null) {
        $data['title'] = 'Gerir utilizadores';
        $output->data = $data;

        $this->load->view('includes/header_crud', (array) $output);
        $this->load->view('crud_view', (array) $output);
        $this->load->view('includes/footer_crud', (array) $output);
    }

    function gerir() {
        try {
            $crud = new grocery_CRUD();
            $crud->set_table('users');
            $crud->set_subject('Utilizador');

            //FORM LAYOUT
            $crud->unset_export();
            $crud->unset_print();
            $crud->columns('username', 'email', 'active', 'last_login');
            $crud->display_as('username', 'Nome')
                    ->display_as('active', 'Estado')
                    ->display_as('first_name', 'Primeiro nome')
                    ->display_as('last_name', 'Último nome')
                    ->display_as('last_login', 'Último login')
                    ->display_as('password_confirm', 'Confirmar password');
            $crud->add_fields('first_name', 'last_name', 'email', 'password', 'password_confirm');
            $crud->edit_fields('first_name', 'last_name', 'email');

            //VALIDATION
            $crud->required_fields('first_name', 'last_name', 'email', 'password', 'password_confirm');
            $crud->set_rules('email', 'E-mail', 'required|valid_email');
            $crud->set_rules('password', 'Password', 'required|matches[password_confirm]');

            //FIELD TYPES
            $crud->change_field_type('last_login', 'readonly');
            $crud->change_field_type('last_login', 'readonly');
            $crud->change_field_type('password', 'password');
            $crud->change_field_type('password_confirm', 'password');

            //CALLBACKS
            $crud->callback_insert(array($this, '_create_user'));
            $crud->callback_update(array($this, 'edit_user'));
            $crud->callback_delete(array($this, 'delete_user'));

            //OUTPUT
            $output = $crud->render();
            $this->_render_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

    function _create_user($post_array) {
        try {
            
            $username = $post_array['first_name'] . ' ' . $post_array['last_name'];
            $password = $post_array['password'];
            $email = $post_array['email'];
            $data = array(
                'first_name' => $post_array['first_name'],
                'last_name' => $post_array['last_name'],
            );

            $this->ion_auth_model->register($username, $password, $email, $data);

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

    function delete_user($primary_key) {
        if ($this->ion_auth_model->delete_user($primary_key)) {
            return true;
        } else {
            return false;
        }
    }

    function edit_user($post_array, $primary_key = null) {
        $username = $post_array['first_name'] . ' ' . $post_array['last_name'];
        $email = $post_array['email'];
        $data = array(
            'username' => $username,
            'email' => $email,
            'first_name' => $post_array['first_name'],
            'last_name' => $post_array['last_name'],
        );

        $this->ion_auth_model->update($primary_key, $data);

        return true;
    }

}

Please, I need your help. Thank you