grocery crud prevent insert on callback before insert function

632 Views Asked by At

i want to prevent grocery crud to insert a new element when there are more than 3 elements on the table with the same client_id. i have already tried to return only false to generate an error, and also setting the values of the column to null, because the element in the database is set to NOT NULL. is there any way to make this?

public function clientes_menu($row)
{
    $data = array(
        "seccion" => "Menú del cliente $row <br/>",
        "seccion_desc" => "<p><a href=\"" . base_url() . "dash/clientes\">Clientes</a> / menú del cliente</p>",
        "admin_name" => $this->session->user['nombre'],
        "admin_email" => $this->session->user['email']
    );
    $crud = new grocery_CRUD();
    $state = $crud->getState();

    $crud->set_table('menu');
    $crud->where(" id_restaurante = $row");
    $crud->order_by('created_at','desc');
    $crud->columns('id_restaurante','platillo','platillo_img');
    $crud->set_field_upload('platillo_img', 'assets/uploads/img_menu');
    $crud->fields('id_restaurante','platillo','platillo_img','is_below_limit');
    $crud->field_type('id_restaurante', 'hidden', $row);
    $crud->field_type('is_below_limit', 'invisible');
    $crud->callback_before_insert(array($this,'cliente_tiene_menu_completo')); 

    $output = $crud->render();
    $output->data = $data;
    $this->_example_output($output);
}
function cliente_tiene_menu_completo($post_array) {
    $this->load->database();
    $id=$post_array['id_restaurante'];
    $q = $this->db->query("SELECT * from menu where id_restaurante =$id;");
    $q = $q->num_rows();
    if($q<=3){
         return $post_array;
    }else{
        $post_array['platillo']=null;
        $post_array['platillo_img']=null;
         return $post_array;
    } 
} 
1

There are 1 best solutions below

0
Luis Antonio Villegas On

i have found the answer i had to add exit(); on my code only instead of the return. so insert will not occur:

function cliente_tiene_menu_completo($post_array) {
    $this->load->database();
    $id=$post_array['id_restaurante'];
    $q = $this->db->query("SELECT * from menu where id_restaurante =$id;");
    $q = $q->num_rows();
    if($q<=3){
         return $post_array;
    }else{
        exit();
    } 
}