Two field as one unique field in Grocery Crud

[亡魂溺海] 提交于 2019-12-07 21:49:06

问题


I have one problem in GroceryCrud.

I want to insert the data in one table. Table is like this:

id, card_no, site_code, site_no

card_no is unique and i have implemented it.

However; my problem is that site_code and site_no should be unique only is both values match. Which means that is site_code is 101 and site_no is 102 and we try to insert identical values for both again is should reject it. If we one of the values is different it should allow it.

In this case we are treating both values as one when we insert it.

I hope I'am clear.

Thanks in advance.


回答1:


Apart from your table design login, you could create a custom callback function to check the existance of these values in your database. The validation will take place in one of your 2 fields, for example site_code:

public function _unique_code_no($site_code)
{
    $CI =& get_instance();
    $CI->form_validation->set_message('_unique_code_no', 'the values must be unique');
    $site_no=$CI->input->post('id');
    $query=$CI->db->query("select 1 from mytable where site_no=$site_no and site_code=$site_code");
    if ($query->num_rows==0){
        return TRUE;
    }else{
        return FALSE;
    }
}

And the validation rule:

$this->form_validation->set_rules('site_code', 'Site code', 'required|_unique_code_no');



回答2:


It sounds like you should look over the constraints on your table.

Is there a good reason for using an id column in this case? If there is not you should remove it and then set both site_code and site_no as your primary key.

With (site_code,site_no) as your primary key yo will not be able to insert duplicates of those two values.




回答3:


I think you should use callback_column( string $column , mixed $callback ) method. Check this link CallBack Column



来源:https://stackoverflow.com/questions/15494526/two-field-as-one-unique-field-in-grocery-crud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!