PHP/MYSQL Transaction generating two same primary key -
i have function used web services. code below.
public function create_order() { try{ $this->db->trans_begin(); $this->form_validation->set_rules($this->form_validation_array->get_app_rules('authenticate')); $this->form_validation->set_rules("table_id","table_id","required"); // $this->form_validation->set_rules("item[]","items","required"); if ($this->form_validation->run() == true) { $credential = array( "restaurant_id" => $this->input->post("restaurant_id"), "app_user_id" => $this->input->post("app_user_id"), "app_id" => $this->input->post("app_id"), "app_secret_id" => $this->input->post("app_secret") ); $check = $this->authenticate_app_user($credential); if($check){ $order_data = array( "user_id" => $credential['restaurant_id'], "table_id" => $this->input->post("table_id"), "created_by" => $credential['app_user_id'], "created_date" => current_timestamp(), 'comments'=> ($this->input->post('comment')) ? $this->input->post('comment') : null, 'status'=>1, ); $order_id = $this->ws_details_model->current_order_id($order_data); log_message('error','order_id_while_inserting '.print_r($order_id,true)); if(!$order_id) { $order_data['unique_order_id'] = $this->common_function->unique_no('order'); $this->common_details_model->complete_existing_order($order_data); $order = $this->ws_details_model->create_order($order_data); } else { $order_data['unique_order_id'] = $order_id->unique_order_id; $order = $order_id->id; } if($order){ //$item_data = json_decode($this->input->post("item"),true); $item_data = $this->input->post("item"); log_message('error','===============order id : '.$order.' started====================\n'); log_message('error',cook_log_message('order_array_without_process',$item_data)); if(is_array($item_data)) { $order_item_data = array(); $j=0; foreach ($item_data $i){ $item_details = $this->ws_details_model->get_item_details($i['item_id']); $toppings = $toppings_price = $varient = $varient_price = $topping_text = $varient_text = ''; $topping_price = 0.00; if(isset($i['item_topping'])) { $topping_details = $this->ws_details_model->get_toppings_details(explode(',',$i['item_topping'])); if(!empty($topping_details)) { foreach($topping_details $topping) { if($topping->is_t_v == 1) { $price = !empty($topping->price) ? $topping->price : '0'; $toppings.= $topping->id.'|'.$topping->title.'|'.$price.','; $topping_text.= $topping->title.','; //$toppings_price.= (!empty($topping->price) ? $topping->price : '0').','; } else { $price = !empty($topping->price) ? $topping->price : '0'; $varient.= $topping->id.'|'.$topping->title.'|'.$price.','; $varient_text.= $topping->title.','; //$varient_price.= (!empty($topping->price) ? $topping->price : '0').','; } $topping_price = $topping_price + $topping->price; } } } if($i['item_id']){ $order_item_data[$j] = array( "order_id" => $order, "item_id" => $i['item_id'], "item_name" => $item_details['title'], "category_name" => $item_details['category_name'], "item_size" => $i['item_size'], "item_quantity" => $i['item_quantity'], "created_by" => $credential['app_user_id'], "created_date" => current_timestamp(), 'comments'=> ($this->input->post('comment')) ? $this->input->post('comment') : null, 'status'=>1, "toppings"=>(!empty($toppings)) ? trim($toppings,',') : null, "toppings_text"=>(!empty($topping_text)) ? trim($topping_text,',') : null, "toppings_price"=>null, "varient"=>(!empty($varient)) ? trim($varient,',') : null, "varient_text"=>(!empty($varient_text)) ? trim($varient_text,',') : null, "varient_price"=>null, ); if($i['item_size'] == 1) { $order_item_data[$j]['item_price'] = $item_details['small_price']; } elseif($i['item_size'] == 2) { $order_item_data[$j]['item_price'] = $item_details['medium_price']; } elseif($i['item_size'] == 3) { $order_item_data[$j]['item_price'] = $item_details['large_price']; } else { $order_item_data[$j]['item_price'] = $item_details['price']; } $order_item_data[$j]['final_price'] = ($order_item_data[$j]['item_quantity'] * $order_item_data[$j]['item_price']) + ($order_item_data[$j]['item_quantity'] * $topping_price); $j++; } } log_message('error',cook_log_message('process_order_array_before_insert',$order_item_data)); $order_item = $this->ws_details_model->insert_order_item($order_item_data); $table_data = array( 'status'=>3, 'updated_by'=>$credential['app_user_id'], 'updated_date'=> current_timestamp() ); $this->ws_details_model->change_table_status($order_data['table_id'],$table_data); if ($this->db->trans_status() === false) { $this->db->trans_rollback(); $data = array('result' => 'error', 'msg' => "something went wrong!!! please try again."); } else { $this->db->trans_commit(); log_message('error','===============order id : '.$order.' end====================\n'); $this->publish($credential['restaurant_id'],'inhouse',array('order_id'=>$order,'order_type'=>'inhouse')); $data = array('result' => 'success', 'msg' => 'order has been placed successfully','order_id'=>$order,'unique_order_id'=>$order_data['unique_order_id']); } } else { $data = array('result' =>'error', 'msg' => "item data not in proper format"); $this->db->trans_rollback(); } } else { $data = array('result' => 'error', 'msg' => "something went wrong!!! please try again."); $this->db->trans_rollback(); } } else { $data = array('result' => 'error', 'msg' => "you not authenticated user"); $this->db->trans_rollback(); } } else { $data = array('result' => 'error', 'msg' => preg_replace("/[\n\r]/", "", strip_tags(validation_errors()))); $this->db->trans_rollback(); } echo json_encode($data); } catch (exception $e) { $this->db->trans_rollback(); log_message('error',cook_log_message('app_create_order_error',$e->getmessage())); } }
now here problem.
- when function called insert row automatically not in
order_item_data
array. know little bit weird if can check out below screenshot understand saying.
to trace out issue have put log system function. when have checked log found out same primary key has generated 2 times. below log
error - 2016-11-12 21:49:07 --> ===============order id : 5091 started====================\n error - 2016-11-12 21:49:07 --> 2016-11-12 21:49:07 : : order array without process : order_array_without_process : array ( [0] => array ( [item_id] => 40 [item_size] => 0 [item_quantity] => 1 [item_topping] => 79 ) ) error - 2016-11-12 21:49:07 --> ===============order id : 5091 end====================\n error - 2016-11-12 22:15:08 --> ===============order id : 5091 started====================\n error - 2016-11-12 22:15:08 --> 2016-11-12 22:15:08 : : order array without process : order_array_without_process : array ( [0] => array ( [item_id] => 65 [item_size] => 0 [item_quantity] => 1 ) [1] => array ( [item_id] => 64 [item_size] => 0 [item_quantity] => 1 ) ) error - 2016-11-12 22:15:08 --> ===============order id : 5091 end====================\n
in above log can see order_id 5091 primary key generated 2 times , if can check error - time of log both have different time how possible ?
below table structure.
1. order table
create table `m_order` ( `id` int(11) not null auto_increment, `user_id` int(11) not null comment 'belongs m_users table', `table_id` int(11) default null comment 'belongs m_table', `unique_order_id` varchar(100) default null, `order_type` int(1) not null default '1' comment '1=table_order,2=take_away', `payment_by` int(11) not null default '0' comment '1=cash,2=card', `order_from` int(11) default '1' comment '1=web,2=app', `device_id` text comment 'device id order placed', `device_name` varchar(254) default null, `delivery_taken_by` int(11) default null comment 'belongs m_staff tables ', `comments` text, `ip_address` varchar(40) default null, `created_by` int(11) not null, `updated_by` int(11) default null, `created_date` int(20) not null, `updated_date` int(20) default null, `status` int(1) not null default '1' comment '1=current order,2=completed,3=order cancelled,4=out delivery', primary key (`id`), key `user_id` (`user_id`), key `table_id` (`table_id`) ) engine=innodb auto_increment=180 default charset=utf8
2. order item table structure
create table `m_order_items` ( `id` int(11) not null auto_increment, `order_id` int(11) not null, `item_id` int(11) not null, `category_name` varchar(200) default null, `item_name` varchar(100) not null, `item_size` varchar(100) not null comment 'small,medium,large', `item_price` float(12,2) not null, `item_quantity` float not null default '1', `item_unit` varchar(255) default null, `toppings` varchar(250) default null, `toppings_text` varchar(255) default null, `toppings_price` varchar(250) default null, `varient` varchar(250) default null, `varient_text` varchar(255) default null, `varient_price` varchar(250) default null, `final_price` float(12,2) default null, `comments` text, `ip_address` varchar(40) default null, `created_by` int(11) not null, `updated_by` int(11) default null, `created_date` int(20) not null, `updated_date` int(20) default null, `is_printed` int(1) not null default '0' comment '1=printed,0=not yet printed', `status` int(1) not null default '1' comment '1=active,2=completed,3=cancelled,4=after serve return,5=deleted', primary key (`id`), key `order_id` (`order_id`), constraint `m_order_items_ibfk_1` foreign key (`order_id`) references `m_order` (`id`) on delete cascade on update cascade ) engine=innodb auto_increment=430 default charset=utf8
so if can understand issue please resolve issue.what should change in code not item array same primary not in posted array value.
thanks in advance.
Comments
Post a Comment