【问题标题】:grocery crud prevent insert on callback before insert function杂货杂货防止在插入函数之前插入回调
【发布时间】:2020-08-07 15:01:46
【问题描述】:

我想防止杂货店杂货插入新元素时,表上有超过 3 个具有相同 client_id 的元素。 我已经尝试仅返回 false 以生成错误,并将列的值设置为 null,因为数据库中的元素设置为 NOT NULL。 有没有办法做到这一点?

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;
    } 
} 

【问题讨论】:

    标签: php function codeigniter sql-insert grocery-crud


    【解决方案1】:

    我找到了答案,我必须在我的代码中添加exit(); 而不是返回。所以插入不会发生:

    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();
        } 
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多