12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace app\commands;
- use v1\models\Estado;
- use v1\models\Municipio;
- use yii\console\Controller;
- use yii\db\Expression;
- use yii\helpers\Json;
- class ImportacionController extends Controller {
- public $truncate = null;
- private $_db = null;
- private $_actions = [
- 'actionProveedores',
- ];
- public function options($actionID) {
- return [
- 'truncate',
- ];
- }
- public function actionIndex() {
- if ($this->truncate == 1) {
- $this->actionTruncate();
- }
- foreach ($this->_actions as $action) {
- $this->{$action}();
- }
- }
- public function actionMunicipios() {
- $this->stdout("Guardando Sincronizar estados\n");
- $basePath = \Yii::getAlias('@app');
- $file = fopen("{$basePath}/csv/municipios.csv", "r");
- if ($file !== false) {
- while (($data = fgetcsv($file, 1000)) !== false) {
- @list($claveEstado, $nombreEstado, $abreviado, $claveMun, $nombreMun) = $data;
- $municipio = null;
- $estado = null;
- $municipio = Municipio::find()
- ->andWhere([
- 'clave' => intval($claveMun),
- 'nombre' => trim($nombreMun)
- ])
- ->orderBy('clave')
- ->one();
- if ($municipio === null) {
- $this->stdout('No se encontró el Municipio');
- continue;
- }
- $this->stdout($municipio->nombre);
- $estado = Estado::find()
- ->andWhere([
- 'clave' => intval($claveEstado),
- 'nombre' => trim($nombreEstado)
- ])
- ->one();
- if ($estado === null) {
- $this->stdout('No se encontró el Municipio');
- continue;
- }
- $municipio->idEstado = $estado->id;
- $this->stdout("{$estado->nombre}\n");
- if (!$municipio->save()) {
- $errores = Json::encode($municipio->getFirstErrors());
- $this->stderr("{$errores}\n");
- }
- }
- }
- fclose($file);
- }
- }
|