小编mah*_*shb的帖子

使用CodeIgniter进行多模型功能的单笔交易

如果在我要回滚已提交查询的任何表中出现任何错误时,我都需要插入2个表中。

我在控制器内部编写了查询,例如:

   $this->db->trans_start();
   $this->db->insert_batch('market_users_mapping', $marketData);
   $this->db->insert_batch('maincategory_users_mapping', $maincategoryData);
   $this->db->trans_complete();
   if ($this->db->trans_status() === FALSE) {
       throw error
   }
Run Code Online (Sandbox Code Playgroud)

这很完美。但是我认为在控制器内部编写查询不是一个好习惯。因此,我做到了这一点,称为模型函数,并在相应的模型函数中编写了这些insert_batch查询。

$this->db->trans_start();
$this->maincategory_model->function_name()
$this->market_model->function_name(); 
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
    throw error
    `enter code here`
} 
Run Code Online (Sandbox Code Playgroud)

但这没有按预期工作

transactions model codeigniter

5
推荐指数
1
解决办法
3052
查看次数

标签 统计

codeigniter ×1

model ×1

transactions ×1