In the Codeigniter Framework, I can validate an Unique field in the MYSQL Database using the "Form Validation Class". Exemple:
在Codeigniter框架中,我可以使用“表单验证类”来验证MYSQL数据库中的一个惟一字段。为例:
$this->form_validation->set_rules('form_field', 'form_label', 'is_unique[table.field]');
Work perfectly, but, I need validate a field from a table with 2 index. Exemple:
工作得很好,但是,我需要从一个有2个索引的表中验证一个字段。为例:
UNIQUE INDEX `id_aluno` (`id_aluno`, `ano`),
The Codeigniter Framework can do it natively?
Codeigniter框架能做吗?
4 个解决方案
#1
4
I don't think that CI
has built-in case for combined PK
but I would use callback_ like this: but note that you have to send the second PK
as extra and the rule should be applied on the first $PK see callbacks for more info about that
我不认为CI有合并PK的内置案例,但是我会使用callback_这样的例子:但是注意,您必须将第二个PK作为额外的,并且规则应该应用于第一个$PK,查看更多关于这个的信息。
$this->form_validation->set_rules('form_field', 'form_label', 'callback_combpk[$pk2]');
public function combpk($pk1, $pk2)
{
$this->db->where('field1', $pk1);
$this->db->where('field2', $pk2);
$result = $this->db->get('table');
if($result->num_rows() > 0)
{
$this->form_validation->set_message('combpk','something'); // set your message
return false;
}
else{ return true;}
}
#2
1
Did not find the description of native support for this functionality in CodeIgniter. You can check after INSERT query the database error number. For example:
在CodeIgniter中没有找到对该功能的本机支持的描述。可以在插入查询后检查数据库错误号。例如:
$last_id = $this->model->set();
if ($last_id === FALSE)
if ($this->db->_error_number() == 1062)
$this->data['message_error'] = 'Not unique.';
else
$this->data['message_error'] = 'Database error.';
This method has disadvantages, but certainly has an advantage - do not use additional SELECT query.
这种方法有缺点,但肯定有优点——不要使用额外的SELECT查询。
P.S. If several different composite unique indexes, then of course you can use preg_match(<pattern_with_index_name>, $this->db->_error_message());
.
如果有几个不同的组合惟一索引,那么当然可以使用preg_match(
#3
1
$this->form_validation->set_rules(
'form_field',
'form_label',
'trima|is_unique[table1.field1],trim|required|is_unique[table.field2]');
use
使用
'trima|is_unique[table1.field1],trim|required|is_unique[table.field2]'
in single code
在单一的代码
#4
1
Maybe you are interested to my custom is_unique function. here it is.
也许您对我的自定义is_unique函数感兴趣。在这儿。
you can use it in 2 ways:
你可以用两种方法:
1. is_unique[table_name.field_to_check.id_field_name.id_field_value] //<-- unique 1 field only
2. is_unique[table_name.field_to_check.id_field_name != 'id_field_value' and anotherIdName='theValue'] //<-- custom where
Just save this code in a file, name it MY_Form_Validation.php, and place it under libraries directory. You can then put those is_unique
将此代码保存在一个文件中,命名为MY_Form_Validation。把它放在库目录下。然后您可以将这些is_unique放入
public function is_unique($str, $field)
{
$result = true;
try {
if ($str) {//validate only if there's a value submitted
$is_query = 0;
$x = substr_count($field, '.'); //count of dots
//ex: is_unique[is_unique[$table_name.$field_name.$field_id!='$id' and 1=1]]
//ex: is_unique[$table_name.$field_name.id!='2' and name!='simson']
if($x == 2) {
list($table, $field, $where) = explode('.', $field);
$is_unique = 0;
if ($where) {
$logos = "select * from $table where $field =? and $where ";
} else {
$logos = "select * from $table where $field =? ";
}
$data = array($str);
$qq = $this->CI->db->query($logos, $data);
$is_query = 1;
$row = $qq->row();
$is_unique = !(bool)$row; //is_unique = (row == empty)
$result = (bool)$is_unique;
}
else {
if ($x >= 3) {
list($table, $field, $id_field, $id_val) = explode('.', $field);
$is_unique = 0;
if ($id_field && $id_val) {
$logos = "select * from $table where $field =? and $id_field != '$id_val' ";
} else {
$logos = "select * from $table where $field =? ";
}
$data = array($str);
$qq = $this->CI->db->query($logos, $data);
$is_query = 1;
$row = $qq->row();
if ($row) {
if ($row->id) {
if ($row->$id_field == $id_val) {
$is_unique = 1; //means editing it self, allow it
} else {
//already exists with different id
}
} else {
//used for left join table
}
} else {
$is_unique = 1; //does not exists
}
$result = (bool)$is_unique;
}
else if ($x == 1) {
list($table, $field) = explode('.', $field);
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
$is_query = 1;
$result = $query->num_rows() === 0;
}
}
if (is_log_query() && $is_query) {
$logos = "logos is_unique x==$x: " . $this->CI->db->last_query();
log_to_file($logos);
} else {
$logos = "logos is_unique x==$x: NOT EXECUTED";
log_to_file($logos);
}
}
}catch (Exception $e) {
die($e->getTraceAsString());
}
return $result;
}
#1
4
I don't think that CI
has built-in case for combined PK
but I would use callback_ like this: but note that you have to send the second PK
as extra and the rule should be applied on the first $PK see callbacks for more info about that
我不认为CI有合并PK的内置案例,但是我会使用callback_这样的例子:但是注意,您必须将第二个PK作为额外的,并且规则应该应用于第一个$PK,查看更多关于这个的信息。
$this->form_validation->set_rules('form_field', 'form_label', 'callback_combpk[$pk2]');
public function combpk($pk1, $pk2)
{
$this->db->where('field1', $pk1);
$this->db->where('field2', $pk2);
$result = $this->db->get('table');
if($result->num_rows() > 0)
{
$this->form_validation->set_message('combpk','something'); // set your message
return false;
}
else{ return true;}
}
#2
1
Did not find the description of native support for this functionality in CodeIgniter. You can check after INSERT query the database error number. For example:
在CodeIgniter中没有找到对该功能的本机支持的描述。可以在插入查询后检查数据库错误号。例如:
$last_id = $this->model->set();
if ($last_id === FALSE)
if ($this->db->_error_number() == 1062)
$this->data['message_error'] = 'Not unique.';
else
$this->data['message_error'] = 'Database error.';
This method has disadvantages, but certainly has an advantage - do not use additional SELECT query.
这种方法有缺点,但肯定有优点——不要使用额外的SELECT查询。
P.S. If several different composite unique indexes, then of course you can use preg_match(<pattern_with_index_name>, $this->db->_error_message());
.
如果有几个不同的组合惟一索引,那么当然可以使用preg_match(
#3
1
$this->form_validation->set_rules(
'form_field',
'form_label',
'trima|is_unique[table1.field1],trim|required|is_unique[table.field2]');
use
使用
'trima|is_unique[table1.field1],trim|required|is_unique[table.field2]'
in single code
在单一的代码
#4
1
Maybe you are interested to my custom is_unique function. here it is.
也许您对我的自定义is_unique函数感兴趣。在这儿。
you can use it in 2 ways:
你可以用两种方法:
1. is_unique[table_name.field_to_check.id_field_name.id_field_value] //<-- unique 1 field only
2. is_unique[table_name.field_to_check.id_field_name != 'id_field_value' and anotherIdName='theValue'] //<-- custom where
Just save this code in a file, name it MY_Form_Validation.php, and place it under libraries directory. You can then put those is_unique
将此代码保存在一个文件中,命名为MY_Form_Validation。把它放在库目录下。然后您可以将这些is_unique放入
public function is_unique($str, $field)
{
$result = true;
try {
if ($str) {//validate only if there's a value submitted
$is_query = 0;
$x = substr_count($field, '.'); //count of dots
//ex: is_unique[is_unique[$table_name.$field_name.$field_id!='$id' and 1=1]]
//ex: is_unique[$table_name.$field_name.id!='2' and name!='simson']
if($x == 2) {
list($table, $field, $where) = explode('.', $field);
$is_unique = 0;
if ($where) {
$logos = "select * from $table where $field =? and $where ";
} else {
$logos = "select * from $table where $field =? ";
}
$data = array($str);
$qq = $this->CI->db->query($logos, $data);
$is_query = 1;
$row = $qq->row();
$is_unique = !(bool)$row; //is_unique = (row == empty)
$result = (bool)$is_unique;
}
else {
if ($x >= 3) {
list($table, $field, $id_field, $id_val) = explode('.', $field);
$is_unique = 0;
if ($id_field && $id_val) {
$logos = "select * from $table where $field =? and $id_field != '$id_val' ";
} else {
$logos = "select * from $table where $field =? ";
}
$data = array($str);
$qq = $this->CI->db->query($logos, $data);
$is_query = 1;
$row = $qq->row();
if ($row) {
if ($row->id) {
if ($row->$id_field == $id_val) {
$is_unique = 1; //means editing it self, allow it
} else {
//already exists with different id
}
} else {
//used for left join table
}
} else {
$is_unique = 1; //does not exists
}
$result = (bool)$is_unique;
}
else if ($x == 1) {
list($table, $field) = explode('.', $field);
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
$is_query = 1;
$result = $query->num_rows() === 0;
}
}
if (is_log_query() && $is_query) {
$logos = "logos is_unique x==$x: " . $this->CI->db->last_query();
log_to_file($logos);
} else {
$logos = "logos is_unique x==$x: NOT EXECUTED";
log_to_file($logos);
}
}
}catch (Exception $e) {
die($e->getTraceAsString());
}
return $result;
}