⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

get PK from one table to the other as FK



ceroberoz
  • profile picture
  • Member

Posted 17 August 2013 - 13:02 PM

I have 2 tables, in tabel A I have id as PK and in tabel B I have aid as FK from A's table.

When I submit form that enter the A's table values, I want table B to get the FK from A's table id.

 

Any sample CI script / guide that I can use? Thanks before.


davidoster
  • profile picture
  • Member

Posted 17 August 2013 - 18:18 PM

This is incomplete explanation [member=ceroberoz].

The first part getting the foreign key can be done by using the callback_after_insert after the user saves (inserts) a record.

But you haven't defined what you want to do with this!

Table B doesn't get by magic the foreign key from the newly inserted record.

If you mean after a new insert on table A when the user inserts a new record on table B then this newly inserted id (from table A) to be used as the id of the foreign key of table's B aid, then this is a completely different issue!!!

 

You need to strictly specify how the aid is getting populated... just imagine the very possible scenario where two users save around the same time two news records on table A. Which value will you store on your aid of table B?


ceroberoz
  • profile picture
  • Member

Posted 17 August 2013 - 19:52 PM

Ok davidoster, pardon me for the incomplete question.

 

What I mean is I want to input some values into "order" table and then set the "billing" to get the primary key from the "order" table in one session.

So when I click the submit button, the primary key (which is based from auto increment order's ID) is distribute automaticaly into the "billing" foreign key table.

 

Sorry for my bad english.


Amit Shah
  • profile picture
  • Member

Posted 18 August 2013 - 09:15 AM

well ceroberoz

 

There is a simple way to deal with the same. Do a callback_after_insert .. this method not only shared you the $post_array but also $primary_key - this is the key that was captured @the last insert point. Do it and you should be able to achieve the tasks.