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,
After looking into this further, I came to the conclusion that it would be best to re-factor my logic. I now save each object individually before assigning the tags to that object and repeat for all objects.
This might not be efficient when there are many objects, but as of now that is not a foreseeable issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With