hola, estoy trabajando con mi sistema em YII y EXTJS4
ahora estoy con un GRUD para importar datos de un archivo CSV.
Pero es un archivo con, algunas veces, mas de 20 mil lineas.
Aparte tiene que hacer un INSERT en dos tablas.
public function actionImportFromCsv()
{
$values = $this->getAttributesRequest();
$idTariffplan = $values['id_tariffplan'];
$idTrunk = $values['id_trunk'];
$handle = fopen($_FILES['file']['tmp_name'], "r");
while($row = fgetcsv($handle)) {
if(isset($row[1]))
{
$prefix = $row[0];
$destination = $row[1];
$price = $row[2];
$modelPrefix = CcPrefix::model()->findAll(array('condition' => "prefix = $prefix"));
if(count($modelPrefix)) {
$modelPrefix = $modelPrefix[0];
$modelPrefix->destination = $destination;
try {
$this->success = $modelPrefix->save();
$errors = $modelPrefix->getErrors();
}
catch (Exception $e) {
$this->success = false;
$errors = $this->getErrorMySql($e);
}
}
else
{
$modelPrefix = new CcPrefix();
$modelPrefix->prefix = $prefix;
$modelPrefix->destination = $destination;
try {
$this->success = $modelPrefix->save();
$errors = $modelPrefix->getErrors();
}
catch (Exception $e) {
$this->success = false;
$errors = $this->getErrorMySql($e);
}
}
if(!$this->success) {
break;
}
$modelRateCard = new CcRatecard();
$modelRateCard->dialprefix = $prefix;
$modelRateCard->rateinitial = $price;
$modelRateCard->id_prefix = $modelPrefix->primaryKey;
try {
$this->success = $modelRateCard->save();
$errors = $modelRateCard->getErrors();
}
catch (Exception $e) {
$this->success = false;
$errors = $this->getErrorMySql($e);
}
if(!$this->success) {
break;
}
}
}
Pero esta muy, pero muy lento, hay alguna forma de optimizar esta función?
Saludos