TypeAhead widget “remote” dataset setting returning “undefined”

A TypeAhead widget I’m using in my Yii2 web application returns “undefined” for the “remote” setting within the dataset options.

Here is my view:


<?php $form = ActiveForm::begin(); ?>


<?= $form->field($game, 'Name')->widget(Typeahead::className(), [

    'dataset' => [

        [

            'prefetch' => Url::to(['game/getgames']), // works fine

            'remote' => Url::to(['game/getgames']) . '?q=%QUERY', // problematic

            'limit' => 10,

            'displayLink' => 'value',

        ]

    ],

    'pluginOptions' => ['highlight' => true],

    'options' => ['placeholder' => 'Type...'],

]); ?>


<?php ActiveForm::end(); ?>

Here is game/getgames controller action:


public function actionGetgames($q = null)

{

    return Json::encode(Game::getDataForJson($q)); // thin controller action calling a model function

}

And here is the code for getting the data in the model (I understand it’s messy, it’s a first pass that I plan on tidying up later on):


public function getDataForJson($q = null)

{

    if ($q === null) { // this section is for prefetch that sends no query

        $games = Game::findAll('');

        if (empty($games)) {

            return array('value' => 'No Data');

        } else {

            $u = array();

            foreach ($games as $game) {

                $u[] = ['value' => $game->Name];

            }

            return $u;

        }

    } else { // this section is for remote that will have a query

        $query = new Query;

        $query->select('Name')->from('game')->where('Name LIKE "%' . $q . '%"')->orderBy('Name DESC');

        $data = $query->createCommand()->queryAll();

        if (empty($data)) {

            return array('value' => 'No Data');

        } else {

            $out = [];

            foreach ($data as $datum) {

                $out[] = ['value' => $datum['Name']];

            }

            return $out;

        }

    }

}

I’m obviously missing something but I’m unsure as to what. From what I can see, I’ve set everything correctly. The prefetch returns everything fine, but the remote only ever returns “undefined”. The problem persists if I turn prefetch off and just have remote working. I can’t see any glaring differences in how the JSON is getting returned to the widget, so it must be some sort difference in how remote processes the data to prefetch?

Any help with this would be appreciated. Thanks.