次の3つのテーブルがあります。
ADDRESSES
------------
// Usual Address Data Fields
People
-------------
// Usual Personal Info
AddressId - FK
RoleId - FK
Role
--------------
RoleId
Name
1 つの Role は複数の People を持つことができ、各 Person は 1 つの Role にのみ所属する必要があります。アドレスに関しては、各人が1つのアドレスにリンクされ、各アドレスは1人にのみ割り当てられます。
だから、私がやりたいのは、人を登録することです。登録フォームは、3 つのテーブルすべてのデータを収集し、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 がないためです。
第二に、ロールのドロップダウン選択をどのように処理できますか?