Codeigniter中的验证UNIQUE字段,具有2个索引 [英] Validation UNIQUE field in Codeigniter with 2 index

查看:273
本文介绍了Codeigniter中的验证UNIQUE字段,具有2个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Codeigniter框架中,我可以使用表单验证类验证MYSQL数据库中的唯一字段。例如:

In the Codeigniter Framework, I can validate an Unique field in the MYSQL Database using the "Form Validation Class". Exemple:

$this->form_validation->set_rules('form_field', 'form_label', 'is_unique[table.field]');

完美地工作,但是,我需要从一个具有2个索引的表中验证一个字段。例如:

Work perfectly, but, I need validate a field from a table with 2 index. Exemple:

UNIQUE INDEX `id_aluno` (`id_aluno`, `ano`),

推荐答案

我不认为 CI 有组合 PK 的内置情况,但我会使用 callback _ 就像这样:但是注意你必须发送第二个 PK 作为额外的规则应该应用在第一个$ PK参见< a href =http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks =nofollow> callbacks 了解更多信息

I don't think that CI has build-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

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

        }

这篇关于Codeigniter中的验证UNIQUE字段,具有2个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆