[英]How to use “is_unique” in Codeigniter and database is an mongodb
how to make an username as unique in validation and output is in "JSON". 如何使用户名在验证和输出中具有唯一性,请参见“ JSON”。 Actually I'm using Codeigniter to build a application and MongoDB as database.
实际上,我正在使用Codeigniter构建应用程序,并将MongoDB作为数据库。 I don't know whether "is_unique" working in mongodb or not but I tried but its not working.
我不知道“ is_unique”是否在mongodb中工作,但是我试过但不工作。
Controller: 控制器:
public function create_customer() { 公共功能create_customer(){
$this->load->helper('form');
$this->load->library('form_validation');
if($this->input->post('username') != $original_value) {
$is_unique = '|is_unique[customer.username]'
} else {
$is_unique = ''
}
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean'.$is_unique);
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('phone_number', 'Phonenumber', 'trim|required|numeric|min_length[10]|max_length[10]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
header('Content-Type: application/json');
echo json_encode(array('success' => 'FALSE'));
}
else
{
$this->load->model('general/customer');
$new_customer[CUSTOMER_FIRST_NAME]=$this->input->post('first_name');
$new_customer[CUSTOMER_LAST_NAME]=$this->input->post('last_name');
$new_customer[CUSTOMER_EMAIL]=$this->input->post('email_address');
$new_customer[CUSTOMER_USERNAME]=$this->input->post('username');
$new_customer[CUSTOMER_PHONE]=$this->input->post('phone_number');
$new_customer[CUSTOMER_PASSWORD]=$this->input->post('password');
$this->customer->is_unique();
$query = $this->customer->add($new_customer);
header('Content-Type: application/json');
echo json_encode(array('success' => 'TRUE'));
}
}
Model: 模型:
public function is_unique()
{
$query = $this->mongo_db->select('username');
$result = mongo_db($query);
$count = mongo_db_num_rows($result);
if ($count>0) {
echo 'Sorry! This Username already exists!';
}
else
{
$insert = $this->mongo_db->insert(CUSTOMER, $new_customer);
return $insert;
}
} }
This is my code. 这是我的代码。 I wanted to keep username as unique in the application.
我想让用户名在应用程序中保持唯一。 Please help me to complete the application.
请帮助我完成申请。
Controller: 控制器:
public function create_customer() 公共功能create_customer()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('phone_number', 'Phonenumber', 'trim|required|numeric|min_length[10]|max_length[10]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
header('Content-Type: application/json');
echo json_encode(array('success' => 'FALSE'));
}
else
{
$this->load->model('general/customer');
$new_customer[CUSTOMER_FIRST_NAME]=$this->input->post('first_name');
$new_customer[CUSTOMER_LAST_NAME]=$this->input->post('last_name');
$new_customer[CUSTOMER_EMAIL]=$this->input->post('email_address');
$new_customer[CUSTOMER_USERNAME]=$this->input->post('username');
$new_customer[CUSTOMER_PHONE]=$this->input->post('phone_number');
$new_customer[CUSTOMER_PASSWORD]=$this->input->post('password');
if($this->customer->is_unique($new_customer[CUSTOMER_USERNAME]))
{
$query = $this->customer->add($new_customer);
header('Content-Type: application/json');
echo json_encode(array('success' => 'TRUE'));
}
else
{
header('Content-Type: application/json');
echo json_encode(array('success' => 'FALSE'));
}
}
}
Model: 模型:
function is_unique($username)
{
$this->mongo_db->where(CUSTOMER_USERNAME, $username);
$query = $this->mongo_db->get(CUSTOMER);
return (count($query) == 0 ? true : false);
}
The model code above by Ganesh UG doesn't exactly work for me. 上面的Ganesh UG所提供的模型代码对我而言并不正确。
Change the return statement to: 将return语句更改为:
return (count((array)$query) == 0 ? true : false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.