【问题标题】:How to use "is_unique" in Codeigniter and database is an mongodb如何在 Codeigniter 和数据库中使用“is_unique”是一个 mongodb
【发布时间】:2014-09-10 11:12:13
【问题描述】:

如何使用户名在验证和输出中具有唯一性是“JSON”。实际上我正在使用 Codeigniter 来构建应用程序和 MongoDB 作为数据库。我不知道“is_unique”是否在 mongodb 中工作,但我试过但它不起作用。

控制器:

公共函数 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'));
}   
} 

型号:

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;
    }

}

这是我的代码。我想让用户名在应用程序中保持唯一。 请帮我完成申请。

【问题讨论】:

  • 什么不起作用?你的结果是什么?不需要编写 is_unique 函数,它内置在 CodeIgniter 的 Form_validation 库中。
  • 实际上一个客户必须有一个用户名,其他客户不应该相同。如果任何客户在我的应用程序中输入了一些用户名,它应该通过抛出错误或消息,如“用户名已经存在”。但现在我在我的应用程序中做了一些修改,现在它工作正常。这是我修改后的代码。

标签: php json mongodb codeigniter


【解决方案1】:

控制器:

公共函数 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'));
        }
     }  
} 

型号:

function is_unique($username)

{
    $this->mongo_db->where(CUSTOMER_USERNAME, $username);
    $query = $this->mongo_db->get(CUSTOMER);
    return (count($query) == 0 ? true : false);
}

【讨论】:

    【解决方案2】:

    Ganesh UG 上面的模型代码并不完全适合我。

    将return语句改为:

    return (count((array)$query) == 0 ? true : false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2011-10-29
      相关资源
      最近更新 更多