I have two tables Accommodation
and Facility
, which are connected in a many-to-many relationship with a third table, Accommodation_facility
.
- Accommodation (accommodation_id, accommodation_type, name)
- Facility (facility_id, facility_name)
- Accommodation_facility (accommodation_id, facility_id)
Using Yii, how can you insert multiple records of data into the Accomodation_facility
table?
Since your question is tagged in "yii" I guess you are using Yii Framework. Take a look at Active Records over at the docs - http://www.yiiframework.com/doc/guide/1.1/en/database.ar
Follow the docs to set up AR classes for your tables, and simply loop over the data you post when you submit your checkboxlist. In this loop you create, populate and save the AR objects for the tables you wish to insert data for.
Inserting using a loop is very slow. Let's say you have 5000 rows to insert, it's going to take around 6 minutes that way (separate insert for each record). It's better to insert the data with a single query:
That will take 1/10 of the time.
You better have to use bindParam to prevent from SQL injections. I don't know if it is the best way to do that, but there is the way i'm doing this :
Hope this helps !
http://www.yiiframework.com/news/72/yii-1-1-14-release-candidate-is-available/