ejemplo de campo dependiente donde campo b depende de a y te trae todos los b que tiene a
Fri Sep 26 2025 13:03:32 GMT+0000 (Coordinated Universal Time)
tablas a: -- Table: public.tipobuques -- DROP TABLE IF EXISTS public.tipobuques; CREATE TABLE IF NOT EXISTS public.tipobuques ( id_tipo integer NOT NULL DEFAULT nextval('tipo_buques_id_tipo_seq'::regclass), desc_tipo character varying COLLATE pg_catalog."default", otros_datos character varying(255) COLLATE pg_catalog."default", CONSTRAINT tipo_buques_pkey PRIMARY KEY (id_tipo) ) TABLESPACE pg_default; ALTER TABLE IF EXISTS public.tipobuques OWNER to postgres; tabla b: -- Table: public.transportes -- DROP TABLE IF EXISTS public.transportes; CREATE TABLE IF NOT EXISTS public.transportes ( id_transporte integer NOT NULL DEFAULT nextval('transportes_id_transporte_seq'::regclass), nombre_transporte character varying(255) COLLATE pg_catalog."default" NOT NULL, id_buquescarga integer, id_tipo integer[], id_nacionalidad integer, otros_datos character varying(255) COLLATE pg_catalog."default", CONSTRAINT transportes_pkey PRIMARY KEY (id_transporte) ) TABLESPACE pg_default; ALTER TABLE IF EXISTS public.transportes OWNER to postgres; <div class="col-xs-4" id='id_transporte' style="display:none;"> <?= $form->field($model, 'id_transporte') ->dropDownList( ArrayHelper::map( Transportes::find()->all(), 'id_transporte', 'nombre_transporte' ), [ 'prompt' => 'Seleccione Transporte', 'id' => 'id-transporte' ] ); ?> </div> <div class="col-xs-4" id='id_tipo' style="display:none;"> <?= $form->field($model, 'id_tipo')->widget(\kartik\depdrop\DepDrop::classname(), [ 'options' => ['id' => 'id-tipo'], 'pluginOptions' => [ 'depends' => ['id-transporte'], 'placeholder' => 'Seleccione Tipo de Buque', 'url' => \yii\helpers\Url::to(['transportes/listartipos']) ], ]); ?> </div> functionController: public function actionListartipos() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $out = []; if (isset($_POST['depdrop_parents'])) { $id_transporte = $_POST['depdrop_parents'][0]; if (!empty($id_transporte)) { // Buscar el transporte seleccionado $transporte = \app\models\Transportes::findOne($id_transporte); if ($transporte && !empty($transporte->id_tipo)) { // Convertir el array PostgreSQL a un array de PHP $ids_tipos = $transporte->id_tipo; // Si es una cadena (formato PostgreSQL array), convertir a array if (is_string($ids_tipos)) { // Remover llaves y convertir a array $ids_tipos = trim($ids_tipos, '{}'); $ids_tipos = $ids_tipos ? explode(',', $ids_tipos) : []; } if (!empty($ids_tipos)) { // Buscar los tipos de buques correspondientes a los IDs en el array $out = \app\models\Tipobuques::find() ->where(['id_tipo' => $ids_tipos]) ->select(['id_tipo as id', 'desc_tipo as name']) ->asArray() ->all(); } } } } return ['output' => $out, 'selected' => '']; }
Comments