123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace app\commands;
- use app\models\Nivel;
- use app\models\NivelMunicipio;
- use yii\console\Controller;
- use yii\db\Expression;
- use yii\db\Query;
- use yii\helpers\Json;
- class InsertController extends Controller {
- public function actionNivelMunicipio() {
- $municipios = (new Query())
- ->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' => '4-A'])
- ->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");
- }
- }
- }
- }
|