I've come into a situation where I need to batch-save objects with their related objects in laravel 4. Essentially what I am doing is a bulk insertion of objects where each object can have many tags (many-to-many relation).
Here is some sample code, notice the TODO comments:
[...]
$batchData = array();
$rowCount = 0;
foreach ($dataArray as $key => $row) {
[...]
// parsing row from CSV
$obj = array();
foreach ($row as $attribute => $value) {
$obj['template_id'] = $templateId;
$obj['batch_id'] = $batchId;
$obj['user_id'] = $confideUserId;
$obj['created_at'] = new \DateTime;
$obj['updated_at'] = new \DateTime;
// Attach Tags if any exist
if ($attribute === 'tags') {
if (!is_null($value) || !is_empty($value)) {
$tags = explode(":", $value);
// TODO: Get tag ID for each tag and add to $obj['tags'] array
}
}
}
// add object to array
$batchData[$rowCount] = $obj;
++$rowCount;
if ($rowCount == \Config::get('app.maxCSV')) {
try {
// TODO: Batch Insert With Related tags??
$obj_model_name::insert($batchData);
} catch (Exception $e) {
return false;
}
$rowCount = 0;
$batchData = array();
}
}
[...]
I could insert each object one-by-one with their relations, but the issue is that we bulk insert these objects via CSV, where we can have anywhere from hundreds to hundreds of thousands of objects.
Anyone have any tips?
FYI the database being used is MSSQL 2012.
Cheers,