I have a form for adding aanbiedingen (offers) from companies. I use three tables for this:
Aanbiedingen
- - - - - - -
idaanbiedingen //id
Aanbieding //offername
Omschrijving //description
Prijs //price
Conditie //condition
Bedrijfaanbiedingen
- - - - - - - - - -
idbedrijfaanbiedingen //id
idaanbiedingen // offers id
idbedrijven // company id
idfotoaanbiedingen // photo id
fotoaanbiedingen
- - - - - - - - -
idfotoaanbiedingen //id
fotonaam // photo name (this is the file_name)
I am able to delete the data from the tables: Aanbiedingen and Bedrijfaanbiedingen. I use the id in the url to delete them. I use the idaanbiedingen in both tables to delete the data.
Question
How can I delete the data from the fotoaanbiedingen
without having the idaanbiedingen in that table? I don't get how to do this. It looks almost impossible. I thought about working with a join on the idfotoaanbiedingen in bedrijfaanbiedingen
but I don't know how to do that.
My model looks like this:
function deleteaanbieding($id)
{
$this->db->where('idaanbiedingen', $id);
$this->db->delete('bedrijfaanbiedingen');
$this->db->where('idaanbiedingen', $id);
$this->db->delete('Aanbiedingen');
redirect('members/aanbiedingen');
}
The $id field is: $id = $this->uri->segment(3);
How can I add the function to delete from fotoaanbiedingen
?
In a standard mysql query it would look something like this.
delete Aanbiedingen,Bedrijfaanbiedingen,fotoaanbiedingen
from Bedrijfaanbiedingen
inner join Aanbiedingen ON Aanbiedingen.idaanbiedingen = Bedrijfaanbiedingen.idaanbiedingen
inner join fotoaanbiedingen ON fotoaanbiedingen.idfotoaanbiedingen = Bedrijfaanbiedingen.idfotoaanbiedingen
where Bedrijfaanbiedingen.idaanbiedingen= {the id}
Please try the code, note this code is not tested. You can convert it to active record if you want, basically table Bedrijfaanbiedingen
has something in common with table fotoaanbiedingen
which is the idfotoaanbiedingen
, you just need to join them.
If you do not have ID field then you can delete using other fields if possible in your situation.
Check if it works for you:
$this->db->join('fotoaanbiedingen', 'fotoaanbiedingen.idfotoaanbiedingen = Bedrijfaanbiedingen.idfotoaanbiedingen');
First I think that it would make your life easier Bedrijfaanbiedingen and fotoaanbiedingen were linked in the other side (fotoaanbiedingen would have a foreign key : idbedrijfaanbiedingen)
For your problem, you should consider using the where_in() activerecord function (see the doc, activerecord section). You have to have an array of idfotoaanbiedingen.
For example, if you have a idbedrijfaanbiedingen you should try something like (the following code is at the beginning of your method):
$this->db->select('idfotoaanbiedingen');
$this->db->from('Bedrijfaanbiedingen');
$this->db->where('idbedrijfaanbiedingen',$yourId);
$query = $this->db->get(); //getting the photo id out of the Bedrijfaanbiedingen table
//not sure about the result array, you should treat it better before entering it in argument of this method
$this->db->where_in('idfotoaanbiedingen',$query->result_array());
$this->db->delete('fotoaanbiedingen');