質問があります。2 つのテーブルの ID で mysql からデータを削除していますが、販売テーブルでは、削除後も同じ ID を保持したいと考えています。そして、新しいセールを追加するときに、最後に削除された ID を保持します。
たとえば、削除時に販売 ID nr 100 があり、新しい販売を作成すると、ID は 101 になります。
これは codeigniter に基づく私のモデルです:
public function getAllInvoiceItems($sale_id)
{
$q = $this->db->get_where('sale_items', array('sale_id' => $sale_id));
if($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}
public function getInvoiceByID($id)
{
$q = $this->db->get_where('sales', array('id' => $id), 1);
if( $q->num_rows() > 0 )
{
return $q->row();
}
return FALSE;
}
public function deleteInvoice($id)
{
$inv = $this->getInvoiceByID($id);
$warehouse_id = $inv->warehouse_id;
$items = $this->getAllInvoiceItems($id);
foreach($items as $item) {
$product_id = $item->product_id;
$item_details = $this->getProductQuantity($product_id, $warehouse_id);
$pr_quantity = $item_details['quantity'];
$inv_quantity = $item->quantity;
$new_quantity = $pr_quantity + $inv_quantity;
$this->updateQuantity($product_id, $warehouse_id, $new_quantity);
}
if($this->db->delete('sale_items', array('sale_id' => $id)) && $this->db->delete('sales', array('id' => $id))) {
return true;
}
return FALSE;
}