简体   繁体   English

杂货杂货防止在插入 function 之前插入回调

[英]grocery crud prevent insert on callback before insert function

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.当桌子上有超过 3 个具有相同 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.我已经尝试仅返回 false 以生成错误,并将列的值设置为 null,因为数据库中的元素设置为 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;
    } 
} 

i have found the answer i had to add exit();我找到了必须添加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();
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM