Php 如何在Codeigniter中获取模型间事务中关系的最后一个id?

Php 如何在Codeigniter中获取模型间事务中关系的最后一个id?,php,mysql,codeigniter,transactions,last-insert-id,Php,Mysql,Codeigniter,Transactions,Last Insert Id,我在用户控制器的一个函数中有这段代码。我正在使用Codeigniter $name_user = $this->input->post('contact-company'); $company_name = $this->input->post('name-company'); $company_orgnr = $this->input->post('orgnr-company'); $phone_company = $this->input->

我在用户控制器的一个函数中有这段代码。我正在使用Codeigniter

$name_user = $this->input->post('contact-company');

$company_name = $this->input->post('name-company');
$company_orgnr = $this->input->post('orgnr-company');
$phone_company = $this->input->post('phone-company');
$email_company = $this->input->post('email-company');

//Insert a a new user                        
$user_info = array(
                'username'  => $email_company,
                'name_user' => $name_user
            );
$company_info = array(
                'name' => $company_name,
                'orgnr' => $company_orgnr,
                'phone' => $phone_company,
                'email' => $email_company
            );

//Insert a new user in db
$query_insertuser = "START TRANSACTION";

//Get sql for inserting a new user
$um = new Usermodel();
$query_insertuser .= $um->getSQLInsert($user_info);

//Get sql for inserting a new company
$cm = new Companymodel();
$query_insertuser .= $cm->getSQLInsert($company_info);

//Get sql for inserting relation between user
//and company (How do I get ID of user and ID of company to use?)
$upm = new Userprofilemodel();
$query_insertuser .= $upm->getSQLInsert();

$query_insertuser .= "COMMIT";

//Do the atual insert
$um->insert($query_insertuser);
它用于通过表单处理注册。(通过表单验证库进行验证)-

  • 用户名为username\u user的用户存储在用户表中
  • 公司存储在公司表中
  • 公司和用户之间的关系存储在 用户配置文件表
我认为代码有点自我解释,但是我不清楚如何在userprofile模型中插入关系。我确实需要用户的最后一个插入id和公司的最后一个插入id,但在我的代码中没有,因为在调用
$upm->getSQLInsert()之前,我实际上没有插入任何用户或公司


还是我做得不对?请给我一些建议…

我显然做得不对。如果有人想麻烦,这是我的解决方案:-)

用户控制器(部分)

用户模型(部分)

公司模式(部分)

用户配置文件模型(部分)

//Insert a new user in db
$um = new Usermodel();

$um->startTransaction();                        
$user_id = $um->insert($user_info);

$cm = new Companymodel();
$company_id = $cm->insert($company_info);

//Insert a new user-profile for newly inserted user
$upm = new Userprofilemodel();
$userprofile_info = array(
                    'user_id' => $user_id,
                    'company_id' => $company_id
                );                        
$upm->insert($userprofile_info);                        
$um->endTransaction();
public function startTransaction() {
        $this->db->trans_start();
}

public function endTransaction() {
        $this->db->trans_complete();
}

public function insert(array $user_info) {
    $this->db->insert('user', $user_info); 
    return $this->db->insert_id();
} 
public function insert(array $company_info) {
    $this->db->insert('company', $company_info); 
    return $this->db->insert_id();
}
public function insert(array $userprofile_info) {
    $this->db->insert('user_profile', $userprofile_info); 
    return $this->db->insert_id();
}