Search code examples
javascriptphpjsonyiiyii2

Cannot get matched row from model in yii


I'm trying to get the related row from user input that is using select2. I'm following this tutorial on how to do it but right now I'm stuck at getting the matched row. This is the select2 to get the input from user :

<?= $form->field($model, 'kod_bangunan')->widget(Select2::classname(), [
                    'data' => ArrayHelper::map(
                        KodModal::find()
                            ->select('kod_bangunan, kod_bangunan')
                            ->where(['aktif' => 'Y'])
                            ->asArray()
                            ->all(), 'kod_bangunan', 'kod_bangunan'
                        ),
                    'language' => 'en',
                    'options' => ['placeholder' => '-- PLEASE SELECT --', 'class' => 'form-control'],
                    'pluginOptions' => [
                        'allowClear' => true,
                        'initialize' => true,
                    ],
                ]);

            ?>

So kod_bangunan is the id that I want the user select from. KodModal is the model for my database kod_modal.

And this is my javascript, I put it at the same file as the above code which is in _capital.php the path is

backend/views/capital-setting/_capital.php

<?php
$script = <<< JS
//here right all javascript 

$('#kodmodal-kod_bangunan').change(function(){
   //kod_bangunan holds the value that is the building code selected
   var kod_bangunan = $(this).val();
   $.get('index.php?r=capitalsetting/get-for-building',{ kod_bangunan : kod_bangunan }, function(data){
       alert(data);
   });
});

JS;
$this->registerJs($script);
?>

and this is my controller file which is CapitalSettingController.php the path is

backend/controllers/CapitalSettingController.php

public function actionGetForBuilding($kod_bangunan){
    $KodModal = KodModal::findOne(['kod_bangunan'=>$kod_bangunan]);
    echo Json::encode($KodModal);
}

so when I choose one kod bangunan from the select2 it suppose to alert the data from row that is matched with kod_bangunan. but right now it shows me error on the console. Here is the error screenshot enter image description here

Please help, I'm new in Yii framework.


Solution

  • can you check why your developer tool shows following request uri?

    capital-setting/index.php?r=kod_modal/capitalsetting/get-for-building&kod_bangunan=008

    i think this should look like:

    capital-setting/get-for-building?kod_bangunan=008

    so try to create the url with yii method:

    $url = Url::to(['capital-setting/get-for-building']);
    <?php
    $script = <<< JS
    //here right all javascript 
    
    $('#kodmodal-kod_bangunan').change(function(){
       //kod_bangunan holds the value that is the building code selected
       var kod_bangunan = $(this).val();
       $.get('$url',{ kod_bangunan : kod_bangunan }, function(data){
           alert(data);
       });
    });
    
    JS;
    $this->registerJs($script);
    ?>
    
    1. option is use pjax

    in your view:

    <?= $form->field($model, 'kod_bangunan')->widget(Select2::class, [
        ...
        'pluginEvents' => [
            "select2:select" => <<<JS
    function onSelect() {
        $.pjax({
        type: 'POST',
        container: '#partial-form-pjax',
        data: $('form').serialize()
        })
    }
    JS
            ,
        ],
    ]); ?>
    
    
    <?php Pjax::begin(['id' => 'partial-form-pjax']) ?>
    
    <?= $form->field($model, 'building_kod')->textInput(); ?>
    
    <?= $form->field($model, 'name_building')->textInput(); ?>
    
    <?php Pjax::end() ?>
    

    in your controller:

    if (Yii::$app->request->isPjax && $model->load(Yii::$app->request->post())) {
        $KodModal = KodModal::findOne(['kod_bangunan'=>$kod_bangunan]);
        $model->kod_building = $KodModal->kod_building;
        $model->name_building = $KodModal->name_building;
        return $this->render('[same view]', compact('model'));
    }