我有以下三个表:
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。
其次,我该如何处理角色下拉选择?