简体   繁体   中英

CodeIgniter - Delete multiple rows and reorder next records in database

I need to delete multiple rows and then reordering the next database records. ELDI_Order it's not an autoincrement.

This is my MODEL for DELETING ONE RECORD

public function delete_one_record($id)
    {

        $this->db->select('ELDI_Order');
        $this->db->where("ELDI_Id", $id);
        $eldi_order = $this->db->get('elem_diccio')->row()->ELDI_Order

        // Delete that record
        $this->db->delete('elementos_diccionario', array('ELDI_Id' => $id));

        // Reorder ELDI_Order for the rest of the records
        $this->db->set('ELDI_Order', 'ELDI_Order-1', FALSE);
        $this->db->where('ELDI_Order >', $eldi_order);
        $this->db->update('elem_diccio');    
    }

MODEL for DELETING MULTIPLE RECORDS

 function eliminar_varios_elementos_diccionario($ids)
    {
        $this->db->where_in('ELDI_Id', explode(",", $ids));
        $this->db->delete('elem_diccion');

        // Code for reordering
    }

For example, if I delete rows 2 and 8 (the 4th) I want to substract 1 to the column ELDI_Order of prueba_3 because it moved "1 place", and 2 from prueba_5 because it "moved" 2 places because I deleted 2 records. 例

例题

Thanks for your help and time!

Just use your delete_one_record function and a foreach. I can't see a way to inefficiently reorder multiple items at a time - so its not too bad to do them individually.

function eliminar_varios_elementos_diccionario($ids)
    {
       if (!is_array($ids)) {
           $ids = explode(",", $ids);
       }
       foreach ($ids as $id) {
           $this->delete_one_record(intval($id)); // assummed function is in same model
       }
    }

Suggestion: think about using transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM