CodeIgniter中的一个复杂输入表单-三个表的数据


A Complex Inputs Form in CodeIgniter - Data for Three Tables

我有以下三个表:

ADDRESSES
------------
// Usual Address Data Fields
People
-------------
// Usual Personal Info
AddressId - FK
RoleId - FK
Role
--------------
RoleId
Name

一个Role可以有多个People,每个Person只能属于一个Role。至于地址,每个人将被链接到一个地址,每个地址将只分配给一个人。

我想做的是Register a Person。Registration表单必须收集所有三个表的数据,并以一种不会使MySQL抛出错误的方式存储它们。

我的表单看起来不错,我有角色填充下拉菜单。但是我的问题是关于提交表单时到达的POST请求的处理。下面是我的POST处理代码:

public function register()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('FirstName', 'First Name', 'trim|required');
    $this->form_validation->set_rules('LastName', 'Last Name', 'trim|required');
    $this->form_validation->set_rules('Telephone', 'Telephone', 'trim|required');
    $this->form_validation->set_rules('Email', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('Password', 'Password', 'trim|required');
    $this->form_validation->set_rules('MaxNo', 'MaxNo', 'trim|required');
    $this->form_validation->set_rules('FirstLine', 'First Line', 'trim|required');
    $this->form_validation->set_rules('SecondLine', 'Second Line', 'trim');
    $this->form_validation->set_rules('City', 'City', 'trim|required');
    $this->form_validation->set_rules('PostCode', 'Post Code', 'trim|required');
    if($this->form_validation->run())
    {
        echo "Validation Failed";
        // something went wrong.
        $this->load->model('rolesmodel');
        $data['roles'] = $this->rolesmodel->getRolesForUserRegForm();
        $this->load->view('shared/header');
        $this->load->view('charity/register', $data);
        $this->load->view('shared/footer');
    } else {
        // All is Good
        $address = array(
            'FirstLine' => $this->input->post('FirstLine'),
            'SecondLine' => $this->input->post('SecondLine'),
            'City' => $this->input->post('City'),
            'PostCode' => $this->input->post('PostCode'),
        );
        $this->load->model('addresses_model');
        $addId = $this->addresses_model->getCurrentCountOfRows();
        $this->addresses_model->addNewsAddress($address);
         $person = array(
            'FirstName' => $this->input->post('FirstName'),
            'LastName' => $this->input->post('LastName'),
            'Telephone' => $this->input->post('Telephone'),
            'Email' => $this->input->post('Email'),
            'Password' => $this->input->post('Password'),
            'MaxNo' => $this->input->post('MaxNo'),
            'AddressId' => (int)$addId + 1,
            'RoleId' => $this->input->post('Name') // this is the Roles DropDown Name Property
        );
        $this->load->model('people_model');
        $this->people_model->registerPerson($person);
        redirect('/animals/index');
    }
}

我知道我必须首先将地址保存到数据库中,但是如果我这样做,我如何才能快速获取其ID?因为它不会有一个ID,除非它首先保存到数据库中。

其次,我如何处理角色下拉选项?

要获得插入后的地址ID,使模型中的函数返回:

return $this->db->insert_id();

为了得到你的下拉值,在你的视图中你会有一个选择。可能是这样的:

<select name="roleId">....</select>

使用这个来获取控制器中的roleId:

$person->RoleId = $this->input->post('roleId')

注意,post()方法中的字符串必须与select

的'name'属性一致

将您的代码更改为:

$this->load->model('PeopleModel');
$this->load->model('AddressModel');
$person->AddressId = $this->AddressModel->insert($address);
$this->PeopleModel->register($person);

希望这对你有帮助,不要犹豫,问更多的问题=)

我将简单地这样做:(这必须是你的寄存器函数,只是伪代码)

 protected function getAddressId($param = array()){
    //save address data here and return it's id;     
 }
 public function register($param = array()){
   //1. grab address id
   $AdsressId = $this->getAddressId()
   //2. save person data + $AddressId + RoleId
   //3. All done
 }