public function applyImport() { $result = $this->checkImport(str_ireplace(['{categoryId}', '{sessionId}'], [$this->categoryId, Yii::$app->session->id], Yii::getAlias(static::IMPORT_FILENAME))); $transaction = $this->db->beginTransaction(); try { // Удаляем записи $deleteItems = Item::findAll($result->deleted); foreach ($deleteItems as $i) { $i->delete(); } // Обновляем записи $updateData = $result->updated; foreach ($result->getUpdatedItems() as $item) { foreach ($updateData[$item->item_id] as $exportIndex => $exportValue) { list($t, $v) = explode('.', $exportIndex); switch ($t) { case static::TYPE_ATTRIBUTE: $item->{$v} = $exportValue[1]; break; case static::TYPE_FIELD: $item->data->{$v} = $exportValue[1]; break; case static::TYPE_SEO: $item->seo->{$v} = $exportValue[1]; break; case static::TYPE_CUSTOM: $this->setCustomExport($exportIndex, $item, $exportValue[1]); break; } } $item->seo->update(); $item->update(); } // Вставляем новые записи foreach ($result->getNew() as $newData) { $item = new Item(); $item->data = new \stdClass(); $seo = []; foreach ($newData as $exportIndex => $exportValue) { list($t, $v) = explode('.', $exportIndex); switch ($t) { case static::TYPE_ATTRIBUTE: $item->setAttributes([$v => $exportValue[1]]); break; case static::TYPE_FIELD: $item->data->{$v} = $exportValue[1]; break; case static::TYPE_SEO: $seo[$v] = $exportValue[1]; break; case static::TYPE_CUSTOM: $this->setCustomExport($exportIndex, $item, $exportValue[1]); break; } } $item->category_id = $this->categoryId; try { if ($item->save()) { foreach ($seo as $key => $value) { $item->seoText->{$key} = $value; } $item->seoText->save(); foreach ($newData as $exportIndex => $exportValue) { list($t, $v) = explode('.', $exportIndex); switch ($t) { case static::TYPE_CUSTOM: $this->afterSetCustomExport($exportIndex, $item, $exportValue[1]); break; } } } } catch (\Exception $e) { if (!$item->isNewRecord) { $item->delete(); } throw $e; } } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } }