杂货店crud在插入函数之前阻止回调中的插入



当表上有3个以上具有相同client_id的元素时,我想防止杂货店crud插入新元素。我已经尝试过只返回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;
} 
} 

我已经找到了答案,我只能在代码中添加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();
} 
} 

最新更新