Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Insert Objects With Relations in Laravel-4

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,

like image 444
Gimli Avatar asked Nov 10 '22 16:11

Gimli


1 Answers

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.

like image 55
Gimli Avatar answered Nov 14 '22 21:11

Gimli