【问题标题】:Validation UNIQUE field in Codeigniter with 2 indexCodeigniter 中具有 2 个索引的验证 UNIQUE 字段
【发布时间】:2013-01-12 13:35:49
【问题描述】:

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

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

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

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

Codeigniter 框架可以原生实现吗?

【问题讨论】:

  • 不确定是否有任何原生的,但是如果添加两次规则会发生什么? $this->form_validation->set_rules('form_field', 'form_label', 'is_unique[table.field1]', 'is_unique[table.field2]');

标签: php mysql codeigniter validation


【解决方案1】:

我不认为CI 具有组合 PK 的内置案例,但我会像这样使用 callback_但请注意,您必须发送第二个 @ 987654324@ 是额外的,该规则应应用于第一个 $PK 参见callbacks 了解更多信息

$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】:
    $this->form_validation->set_rules(
        'form_field', 
        'form_label', 
        'trima|is_unique[table1.field1],trim|required|is_unique[table.field2]');
    

    使用

    'trima|is_unique[table1.field1],trim|required|is_unique[table.field2]' 
    

    在单个代码中

    【讨论】:

    • 检查每个单独的值,而不是作为组合索引。
    【解决方案3】:

    在 CodeIgniter 中没有找到对该功能的原生支持的描述。您可以在 INSERT 查询后检查数据库错误号。例如:

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

    这种方法有缺点,但肯定有一个优点——不要使用额外的 SELECT 查询。

    附:如果有几个不同的复合唯一索引,那么当然可以使用preg_match(<pattern_with_index_name>, $this->db->_error_message());

    【讨论】:

      【解决方案4】:

      也许你对我的自定义 is_unique 函数感兴趣。 在这里。

      您可以通过两种方式使用它:

      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 
      

      只需将此代码保存在一个文件中,将其命名为 MY_Form_Validation.php,并将其放在库目录下。然后你可以把那些 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;
          }
      

      【讨论】:

        猜你喜欢
        • 2013-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2015-04-19
        • 1970-01-01
        相关资源
        最近更新 更多