在這一刻把我的頭髮撕掉,希望有人能幫助我!Yii2 Kartik-V Typeahead高級小工具:如何在Mysql中存儲結果
我正在用Yii2使用Kartik-V Typeahead高級部件。
該插件的工作原理是功能在頁面上完美運行,我搜索並將結果顯示在自動完成列表中。
不幸的是,我無法在012數據庫中保存結果。我在以下行中看到一個問題:
->where([ 'name' => $model->name ])//This variable is returning null
我試圖不正確地存儲數據嗎?我試過了我能想到的所有東西,但我相信這裏有人會想出更好的東西!
請參閱下面的完整代碼。
我的控制器:
public function actionIndex()
{
$model = new Member();
if ($model->load(Yii::$app->request->post())) {
$test = Test::find()
->where([ 'name' => $model->name ])//This variable is returning null
->one();
$test->updateCounters(['times_used' => 1]);
}
return $this->render('index', [
'model' => $model,
]);
}
/*************
* Initial prefetch of results
*************/
public function actionPrefetchlist() {
$query = new Query;
$query->select('name')
->from('test_table')
->limit(10)
->orderBy('times_used');
$command = $query->createCommand();
$data = $command->queryAll();
$out = [];
foreach ($data as $d) {
$out[] = ['value' => $d['name']];
}
echo Json::encode($out);
}
/*************
* Remote results
*************/
public function actionRemotelist() {
$query = new Query;
$query->select('name')
->from('test_table')
->where('name LIKE "%' . $q .'%"')
->limit(10)
->orderBy('times_used');
$command = $query->createCommand();
$data = $command->queryAll();
$out = [];
foreach ($data as $d) {
$out[] = ['value' => $d['name']];
}
echo Json::encode($out);
}
視圖文件:
echo $form->field($model, 'name')->label(false)->widget(Typeahead::classname(), [
'name' => 'name',
'options' => ['placeholder' => 'Filter as you type ...'],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
'display' => 'value',
'prefetch' => Url::to(['prefetchlist']),
'remote' => [
'url' => Url::to(['remotelist']) . '?q=%QUERY',
'wildcard' => '%QUERY'
]
]
]
]);
謝謝,這對我很有用!順便說一句,你忘了$ q到actionRemoteList參數 – Nan