![]() |
[DB]Transaction - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: [DB]Transaction (/showthread.php?tid=67013) |
[DB]Transaction - wishmaster - 01-02-2017 Hi, I have one method wich adds new item's name to another table and returns ID of this name. When name is present use the old ID of name. Q1: do I need use $this->db->trans_begin() when I want rollback transaction in any time or may use $this->db->trans_start()? Q2: is my implementation of adding new items and rollbacking transaction is safe? private function addItemName(string $name) { $this->db->trans_start(); $this->db->query("SELECT @maxid := max(`fin_id`) from `fitem_name_" . $this->user_lib->filialId() . "` FOR UPDATE"); $this->db->query("SET @maxid = IFNULL(@maxid,0) + 1"); $sql = "INSERT INTO `fitem_name_" . $this->user_lib->filialId() . "` SET" . " `fin_id`=@maxid" . ",`fin_name`=" . $this->db->escape($name); $this->db->query($sql); $query = $this->db->query("SELECT max(`fin_id`) AS `maxid` from `fitem_name_" . $this->user_lib->filialId() . "`"); $this->db->trans_complete(); if ($this->db->trans_status() === FALSE) { return FALSE; } else { return $query->result_object()[0]->maxid; } } } public function aditContract (array $data): bool { ... some code ... $this->db->trans_begin(); //$this->db->trans_start(); $item_name = $this->getItemNameByName($item_val['name']); if ($item_name) { $item_name_id = $item_name[0]->fin_id; } else { $add_name = $this->addItemName($item_val['name']); if ($add_name === FALSE) { $this->db->trans_rollback(); return FALSE; } else { $item_name_id = $add_name; } } ... some code ... } |