I'm developing an app that will need to import data for three models from a single CSV file (with one-to-many associations). I have set up a Datafile model & controller to handle upload / parsing of the file. Right now, all the logic for parsing and saving the records is in the controller. This allows me to save to several different models, get the IDs for the saved records, and create associations as necessary while the file is being parsed.
Thinking about the "fat model, skinny controller" principle, I realized I have about 150 lines of code in a controller that is really just processing data. As I started looking at moving this to a model, though, I concluded I would have to process all of this data into arrays (without knowing association IDs), and send it back to the controller to be saved (since the model can't call methods from other models). I'm anticipating having about 1,500 records in the import file. I'm using CakePHP, which has a saveAll()
method to save data to several models simultaneously from a single array.
Another option would be to have each of the three models separately parse the file, ignoring any data it doesn't need. This should be possible, as long as I send it to the models in the right order, and give the "belongsTo" models a list of possibly associated records to search.
So -- any advice on these options?
- Leave the parsing code in the Datafile controller as it is.
- Move all the parsing code to the Datafile model, then pass back a large array to be saved through the Datafile controller.
- Send the file separately to each of the three models, along with supplemental lists for determining associations.