【问题标题】:Use CodeIgniter is_unique form validation with Postgres Table in a schema在模式中使用 CodeIgniter is_unique 表单验证和 Postgres 表
【发布时间】:2020-06-03 13:21:16
【问题描述】:

我的数据库是 Postgres,我已按模式组织表。当我尝试使用 is_unique 表单验证规则时,它不起作用。例如,如果我希望用于唯一检查的表是 products.catalog,而我要使用的列是 name。当我运行如下验证时。

$this->form_validation->set_rules("name", "Name", 'required|is_unique["products.catalog.name"]');

$this->form_validation->run();

我收到这样的错误

Error Number: 42P01/7

ERROR: relation "products" does not exist LINE 2: FROM "`products" ^

SELECT * FROM "products" WHERE "catalog" = 'bags' LIMIT 1

Filename: libraries/Form_validation.php

Line Number: 1122

虽然我可以使用原始 PHP 来检查自己,但我想知道 CodeIgniter 是否提供了解决此问题的方法。

【问题讨论】:

    标签: php postgresql codeigniter codeigniter-3 codeigniter-form-validation


    【解决方案1】:

    您可以在“/system/libraries/Form_validation.php”中创建自己的“is_unique”函数

     public function is_unique_with_schemas($str, $field)
        {
            list($table, $field)=explode('_', $field);
            $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
    
            return $query->num_rows() === 0;
        }
    

    我改变了分隔符。到_

    然后您可以使用这些新函数 'is_unique_with_schemas[schema.tableName_columnName]。

    p.s 不要忘记设置新的“错误信息”

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多