在模式中使用 CodeIgniter is_unique 表单验证和 Postgres Table

Use CodeIgniter is_unique form validation with Postgres Table in a schema

我的数据库是 Postgres,我在模式中组织了我的 table。当我尝试使用 is_unique 表单验证规则时,它不起作用。例如,如果我希望用于唯一检查的 table 是 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 是否提供了解决此问题的方法。

您可以在'/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别忘了设置新的'error message'