Welcome Guest, Not a member yet? Register   Sign In
Insert one form in two tables
#1

[eluser]Unknown[/eluser]
Hello,

I'm a newbie with CI but looking forward to use it in my next project.

I've checked some tutorials, read part of the documentation, checked the forum and googled a little...

What I can't find out is how to code the following situation:

I have a page with a form containing around 10 questions (text field & select boxes) that should be inserted in one table (table2), and other 10 questions (select boxes) that should be inserted in another table (table2)...

Any comments? Thanks in advance!
#2

[eluser]Dam1an[/eluser]
Hi, welcome to CI

Assuming you use one model per database table, I would do
1) Submit the form to your controller and perform any validation
2) Prep the data for table1 and pass it into your model
3) Do the same for the other data and pass it into the model for table2 to insert

The problem might be if you have a strict controler -> model relationship, and doing table2 stull in controller1 might seem off... Is this the case?
#3

[eluser]kgill[/eluser]
The whole 1 table to 1 model thing is the wrong way to go for this problem, a model is supposed to be a representation of a real world object, your models can and should use more than one table where it makes sense. For example I have a student model and it touches 4 tables because while my data is normalized the object I'm interacting with shouldn't be. The controller doesn't need to have any understanding of the underlying data structure so that said simply pass the whole mess to the model and have it deal with it using two insert statements, one to table1 and one to table2 - it's the model's job to know how to deal the data.
#4

[eluser]Unknown[/eluser]
Hi,

Thanks for the answers... now I have all more clear...

I will try to make it work...

Regards,
A.




Theme © iAndrew 2016 - Forum software by © MyBB