Welcome Guest, Not a member yet? Register   Sign In
Copy and mutiple insert into (mysql)? (with or wothout active record)
#1

[eluser]bastien31[/eluser]
Hi everyone,

My problem is not directly linked to CI but maybe is there a neasy solution using active record.

My problem is very simple to explain but I don't success to find the right answer to it !

I have 3 tables in mysql, called tA, tB, tC. I have one entry A in tA, 2 entries B in tB and 3 entries C in tC with a hierarchical architecture like this:

A
|
---------
| |
B1 B2
| |
----- |
| | |
C1 C2 C3

As you can imagine, I have B entries are linked to A entries with A id, and C entries are linked to B entries with B id.

I just want to copy these data to :

A'
|
---------
| |
B1' B2'
| |
----- |
| | |
C1' C2' C3'

So at the beginning, I start by creating A' and copying the B entries linked to A'. No worries.

But after, I did a request with a left join to copy C entries to C'. It nearly works... the only problem is that, by this way, my C' entries are linked to B id and not to B' id !!!!

I try some other stuffs but I don't know how to do that. It seems so simple. Maybe I'm tired... Maybe I have to copy B entries one by one, and copy all C entries of the current B entry before to go to the next.

But is there not a smarter way to do that ? With insert_id, cascade, trigger, foreign key ???

I'm not a sql specialist and I hope somebody here will have a nice solution.

Thanks by advance.

Bastien
#2

[eluser]bgreene[/eluser]
perhaps the problem is because the a,b,c tables have auto-increment fields for the id column in which case the a',b',c' tables should not have auto-increment as this will give each entry a new id and thereby break the links




Theme © iAndrew 2016 - Forum software by © MyBB