/** * Obtener las provincias de un determinado País. * @param integer $pais_id * */ public function actionProvinciasPorPais($pais_id) { $items = []; if (!empty($pais_id)) { $items = ArrayHelper::map(Provincia::find()->where(['pais_id' => $pais_id])->orderBy('nombre ASC')->asArray()->all(), 'id', 'nombre'); } return \yii\helpers\Json::encode($items); }
public function search($params) { $query = Provincia::find()->joinWith('pais'); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->getSort()->attributes = array_merge($dataProvider->getSort()->attributes, ['pais_nombre' => ['asc' => ['pais.nombre' => SORT_ASC], 'desc' => ['pais.nombre' => SORT_DESC], 'label' => Yii::t('app', 'País')]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'nombre', $this->nombre])->andFilterWhere(['like', 'pais.nombre', $this->pais_nombre]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getProvincia() { return $this->hasOne(Provincia::className(), ['id' => 'provincia_id']); }
/** * @return \yii\db\ActiveQuery */ public function getProvincias() { return $this->hasMany(Provincia::className(), ['pais_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getIdProv() { return $this->hasOne(Provincia::className(), ['id_prov' => 'id_prov']); }
/** * Finds the Provincia model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Provincia the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Provincia::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
?> <?php if ($model->isNewRecord) { ?> <?php echo $form->field($model, 'provincia_id')->dropDownList(['' => 'Seleccione un País'], ['prompt' => '']); ?> <?php } else { ?> <?php echo $form->field($model, 'provincia_id')->dropDownList(ArrayHelper::map(Provincia::find()->where(['pais_id' => $model->provincia->pais_id])->innerJoinWith('ciudades')->orderBy('nombre ASC')->asArray()->all(), 'id', 'nombre')); ?> <?php } ?> <?php echo $form->field($model, 'nombre')->textInput(['maxlength' => 60]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div>