views:

253

answers:

1

Hi Stackoverflow,

I am writing a component and I have a scenerio of storing data into multiple tables with One-To-Many relationship.

Master table is Student and Child table is Student_Subjects.
One student can select multiple subjects.

I want to store the data in Student_subject table with student. Data is submitted from one form where user will be created and subjects will be selected.

Currently I am using JTable Class.

Is there any way to run a transaction query to store the data in both tables? If Yes, then how can i get the student id (auto generated) store it into Student_Subject using the same transaction query batch?

Please guide. Example with code is highly appreciated and needed.

A: 

Why would you want to run that simultaneously? You can run 2 queries to do this. First insert your "Student" like so:

$db =& JFactory::getDBO();
$db->setQuery( $query );
$db->query();

Then get the id of the last inserted element like so:

$student_id = $db->insertid();

Now that you have the master id you can do your next insert.

Martin