Sono entrato in una situazione in cui ho bisogno di salvare in batch gli oggetti con i relativi oggetti in laravel 4. In sostanza quello che sto facendo è un inserimento di massa di oggetti in cui ogni oggetto può hanno molti tag (relazione molti-a-molti).Inserimento batch di oggetti con relazioni in Laravel-4
Ecco alcuni esempi di codice, notare i commenti TODO:
[...]
$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();
}
}
[...]
ho potuto inserire ogni oggetto uno per uno con le loro relazioni, ma la questione è che noi Inserimento di massa di questi oggetti tramite CSV, dove possiamo avere ovunque da centinaia a centinaia di migliaia di oggetti.
Qualcuno ha qualche suggerimento?
FYI il database utilizzato è MSSQL 2012.
Cheers,