【问题标题】:Yii2 Select2 multiple with ajax: Illegal offset typeYii2 Select2 使用 ajax 多重:非法偏移类型
【发布时间】:2018-12-23 23:03:16
【问题描述】:

我使用 Yii2 和小部件 Select2。我希望onkeyup 从表“产品”中搜索具有多项选择选项的产品,因为我必须将结果保存在第二个表“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;
}

【问题讨论】:

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


    【解决方案1】:

    模型变化:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-12
      • 2013-03-24
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 2019-07-16
      • 1970-01-01
      相关资源
      最近更新 更多