Codeigniter 不允许我更新条目,因为某些字段必须是唯一的
Codeigniter doesn't let me update entry, because some fields must be unique
所以我想做的是:
从数据库中提取用户数据,包括电子邮件地址、用户名等
编辑它
- 保存
但我想保持用户名和电子邮件的唯一性。
为此,我设置了这样的验证规则:
$this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password1', 'Password', 'required|matches[password2]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'required');
$this->form_validation->set_rules('group', 'User Group', 'required');
如您所见,我有 is_unique[users.username]
和 is_unique[users.email]
规则。但它不允许我使用此规则更新我的条目。
所以问题是,我怎样才能更新数据库条目,并使这 2 个字段保持唯一(用户名和电子邮件)?
使用回调验证函数
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_user_email');
function check_user_email($email) {
if($this->input->post('id'))
$id = $this->input->post('id');
else
$id = '';
$result = $this->user_model->check_unique_user_email($id, $email);
if($result == 0)
$response = true;
else {
$this->form_validation->set_message('check_user_email', 'Email must be unique');
$response = false;
}
return $response;
}
在模型中
function check_unique_user_email($id = '', $email) {
$this->db->where('email', $email);
if($id) {
$this->db->where_not_in('id', $id);
}
return $this->db->get('user')->num_rows();
}
使用相同的用户名....
在 Codeigniter 4 中
// is_unique[table.field,ignore_field,ignore_value]
$validation->setRules([
'name' => "is_unique[supplier.name,uuid, $uuid]", // is not ok
'name' => "is_unique[supplier.name,uuid,$uuid ]", // is not ok
'name' => "is_unique[supplier.name,uuid,$uuid]", // is ok
'name' => "is_unique[supplier.name,uuid,{uuid}]", // is ok - see "Validation Placeholders"
]);
例如
$validation->setRules(['email' => 'required|valid_email|is_unique[users.email,id,4]']);
// 'id' = ignore table field name (users table 'id' field)
// '4' = ignore field value(currnet user id)
对于 Codeigniter 3
添加以下助手class (edit_unique_helper.php)
函数edit_unique($value, $params) {
$CI =& get_instance();
$CI->加载->数据库();
$CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");
list($table, $field, $current_id) = explode(".", $params);
$query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();
if ($query->row() && $query->row()->id != $current_id)
{
return FALSE;
} else {
return TRUE;
}
}
加入自动加载()
使用 edit_unique 传递额外参数,即您正在编辑的行的 ID
例如。 $this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
所以我想做的是:
从数据库中提取用户数据,包括电子邮件地址、用户名等
编辑它
- 保存
但我想保持用户名和电子邮件的唯一性。 为此,我设置了这样的验证规则:
$this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password1', 'Password', 'required|matches[password2]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'required');
$this->form_validation->set_rules('group', 'User Group', 'required');
如您所见,我有 is_unique[users.username]
和 is_unique[users.email]
规则。但它不允许我使用此规则更新我的条目。
所以问题是,我怎样才能更新数据库条目,并使这 2 个字段保持唯一(用户名和电子邮件)?
使用回调验证函数
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_user_email');
function check_user_email($email) {
if($this->input->post('id'))
$id = $this->input->post('id');
else
$id = '';
$result = $this->user_model->check_unique_user_email($id, $email);
if($result == 0)
$response = true;
else {
$this->form_validation->set_message('check_user_email', 'Email must be unique');
$response = false;
}
return $response;
}
在模型中
function check_unique_user_email($id = '', $email) {
$this->db->where('email', $email);
if($id) {
$this->db->where_not_in('id', $id);
}
return $this->db->get('user')->num_rows();
}
使用相同的用户名....
在 Codeigniter 4 中
// is_unique[table.field,ignore_field,ignore_value]
$validation->setRules([
'name' => "is_unique[supplier.name,uuid, $uuid]", // is not ok
'name' => "is_unique[supplier.name,uuid,$uuid ]", // is not ok
'name' => "is_unique[supplier.name,uuid,$uuid]", // is ok
'name' => "is_unique[supplier.name,uuid,{uuid}]", // is ok - see "Validation Placeholders"
]);
例如
$validation->setRules(['email' => 'required|valid_email|is_unique[users.email,id,4]']);
// 'id' = ignore table field name (users table 'id' field)
// '4' = ignore field value(currnet user id)
对于 Codeigniter 3
添加以下助手class (edit_unique_helper.php)
函数edit_unique($value, $params) { $CI =& get_instance(); $CI->加载->数据库();
$CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used."); list($table, $field, $current_id) = explode(".", $params); $query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get(); if ($query->row() && $query->row()->id != $current_id) { return FALSE; } else { return TRUE; }
}
加入自动加载()
使用 edit_unique 传递额外参数,即您正在编辑的行的 ID 例如。
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');