select([ '{{Municipio}}.id as id', '{{Municipio}}.nombre as nombre' ]) ->from('Municipio') ->innerJoin('Estado', '{{Municipio}}.[[idEstado]] = {{Estado}}.id') ->andWhere(['{{Municipio}}.eliminado' => null]) ->andWhere(['{{Estado}}.eliminado' => null]) /* ->andWhere(['{{Estado}}.nombre' => 'TAMAULIPAS']) */; $nivel = Nivel::find() ->andWhere(['eliminado' => null]) ->andWhere(['clave' => '5']) ->andWhere(['nombre' => 'Nivel 5-B3']) ->one(); foreach($municipios->each() as $municipio) { $this->stdout("Procesando {$municipio['nombre']}\n"); $existe = NivelMunicipio::find() ->andWhere(['[[idMunicipio]]' => $municipio['id']]) ->andWhere(['eliminado' => null]) ->exists(); if ($existe) { continue; } $nivelMunicipio = new NivelMunicipio(); $nivelMunicipio->uuid(); $nivelMunicipio->idMunicipio = $municipio['id']; $nivelMunicipio->idNivel = $nivel->id; $nivelMunicipio->creado = new Expression('now()'); if (!$nivelMunicipio->save()) { $errores = Json::encode($nivelMunicipio->getFirstErrors()); $this->stdout("Ocurrió un error con el municipio {$municipio['nombre']}: {$errores}\n"); } } } }