Hier wird die Basis-one :)
SiteController.php (Controller-Datei):
use yii\data\ActiveDataProvider;
use Yii;
class SiteController extends yii\web\Controller
{
public function actionSearch() {
$searchString = Yii::$app->request->get('query');
$searchQuery = Product::find()->where(['name' => $searchString])->orderBy(['ts' => SORT_DESC]);
$productDataProvider = new ActiveDataProvider(['query' => $searchQuery]);
return $this->render('search', compact('productDataProvider'));
}
search.php (View-Datei):
<?php
use yii\helpers\Url;
use yii\grid\GridView;
$form = \yii\widgets\ActiveForm::begin([
'options' => ['role' => 'form', 'method' => 'GET', 'action' => Url::to(['site/search'])]
]); ?>
<?=$form->field($model, 'query')?>
<?=\yii\helpers\Html::submitButton('Поиск')?>
<?php $form->end()?>
<? if ($productDataProvider->totalCount): ?>
<?= GridView::widget([
'dataProvider' => $productDataProvider,
'columns' => [
'id',
'name'
],
]) ?>
<? else: ?>
<p>Ничего не найдено</p>
<? endif; ?>
tun Sie möchten das Ergebnis der Abfrage in der Gridview sehen? – scaisEdge