带有ajax的Yii2 Select2倍数:非法偏移类型

时间:2018-07-16 11:48:31

标签: yii2 jquery-select2 yii2-advanced-app

我使用Yii2和小部件Select2。我希望onkeyup从表“ Products”中搜索带有多个选择选项的产品,因为我必须将结果保存在第二个表“ rel_products”中。我知道为什么它会返回错误:"Illegal offset type"

这是模型:

    public $products = array();   =>because i write result in second table

这是视图:

  $url = \yii\helpers\Url::to(['/product/prodlist']);
            echo $form->field($model, 'products')->widget(Select2::classname(), [
                'initValueText' => 'Search for a city ...', // set the initial display text
                'model' => $model,
                'attribute' => 'products',
                'theme' => 'bootstrap',
                'options' => ['placeholder' => 'Search for a city ...'],
                'pluginOptions' => [
                    'allowClear' => true,
                    'minimumInputLength' => 3,
                    'ajax' => [
                        'url' => $url,
                        'dataType' => 'json',
                        'data' => new JsExpression('function(params) { return {q:params.term}; }')
                    ],
                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                    'templateResult' => new JsExpression('function(product) { return product.text; }'),
                    'templateSelection' => new JsExpression('function (product) { return product.text; }'),
                ],
            ]);

这里是控制器:

 public function actionProdlist($q = null, $id = null) {

    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $out = ['results' => ['id' => '', 'text' => '']];
    if (!is_null($q)) {
        $query = new Query;
        $query->select()
            ->from('product')
            ->joinWith('translation')
            ->where(['like', 'title', $q])
            ->limit(20);
        $command = $query->createCommand();
        $data = $command->queryAll();
        $out['results'] = array_values($data);
    }
    elseif ($id > 0) {
        $out['results'] = ['id' => $id, 'text' => Product::find($id)->title];
    }
    return $out;
}

1 个答案:

答案 0 :(得分:1)

型号更改:

 public $products;   =>because i write result in second table