Delete data from 3 different tables (Codeigniter)

Asked

Viewed 122 times

0

I am trying to delete photos from a view, however, I need to delete beyond the photo table the data from two more tables that are linked to this photo.

The code with the delete function is as follows:

function deleteImage($id)
{
    $this->db->select('idImagem')->from('tbdimagem')->where('idImagem', $id);
    $subQuery =  $this->db->get_compiled_select();

    $tabelas = array('tbdsubcategoria', 'tbdcategoria', 'tbdimagem');
    $this->db->where('idImagem', $subQuery);
    $this->db->delete($tabelas);
}

But it returns this when executing the function in the view:

inserir a descrição da imagem aqui

EDIT 1

inserir a descrição da imagem aqui

EDIT 2

Table Image

Tabela Imagem

Table Category

inserir a descrição da imagem aqui

Table Subcategory

inserir a descrição da imagem aqui

2 answers

0

I’d do it this way

function deleteImage($id){

    $this->db->where('idImagem', $id);
    $delete_tbdsubcategoria = $this->db->delete('tbdsubcategoria');

    $this->db->where('idImagem', $id);
    $delete_tbdcategoria = $this->db->delete('tbdcategoria');   

    $this->db->where('idImagem', $id);
    $delete_tbdimagem = $this->db->delete('tbdimagem');

}
  • I edited my post with a print of the result on this code you sent

  • Post also your table structure, for us to analyze... because it says there are no images in the table indicated.

  • I posted the tables. Just to be clear, in the table Tbdimagem the field idImage is primary key, and in the tables Tbdcategoria and Tbdsubcategoria the field idImage is foreign key.

0


I managed to solve, in the two tables 'daughters' I had to put the parameter 'on delete' in foreign keys. After that, I was able to delete the data from the 'parent' table'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.