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.
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');
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.
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