0

我有以下三个表:

ADDRESSES
------------
// Usual Address Data Fields

People
-------------
// Usual Personal Info
AddressId - FK
RoleId - FK

Role
--------------
RoleId
Name

一个角色可以有很多人,每个人只能属于一个角色。至于地址,每个人都会链接到一个地址,每个地址只会分配给一个人。

所以,我想做的是注册一个人。注册表单必须收集所有三个表的数据并以不会使 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。

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

4

2 回答 2

1

要在插入后获取您的地址 ID,请让模型中的函数返回:

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

要获得您的下拉值,在您的视图中您将有一个选择。大概是这样的:

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

使用它在您的控制器中获取您的 roleId:

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

请注意,您在 post() 方法中输入的字符串必须符合您选择的“名称”属性

最后将您的代码更改为:

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

希望这会有所帮助,不要犹豫再问更多问题=)

于 2013-04-29T11:06:58.597 回答
1

我会这样做:(这必须是你的注册函数,只是伪代码)

 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
 }
于 2013-04-29T12:14:30.687 回答