Нужно сделать поиск по таблице users через форму поиска внутри моей админки.
В users.php есть вывод всех пользователей из базы данных. Не понимаю как сделать поиск по этому полученному выводу, чтобы осталось только то значение, которое я указал в форме поиска. Значение по которому буду искать - email.
Searchuserme.php - модель
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class extends ActiveRecord
{
public static function tableName()
{
return '{{users}}';
}
}
SearchusermeController.php - контроллер
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\helpers\Url;
use app\models\Blog;
use app\models\Categories;
use app\models\User;
use app\models\Searchuserme;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
class SearchusermeController extends Controller
{
public function actionSearch($params)
{
$q = trim(Yii::$app->request->get('q'));
if(!$q) return $this->render('search');
$query = Searchuserme::find()->where(['email', $q]);
/*$pages = new Pagination([
'totalCount' => $query->count(),
'pageSize' => 1,
'forcePageParam' => false,
'pageSizeParam' => false
]);*/
$users = $query->orderBy(' DESC')
->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('search', compact('email','q'));
}
}
users.php - view
<?php
namespace app\controllers;
use Yii;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use dosamigos\fileupload\FileUploadUI;
use app\models\User;
use app\models\Searchuserme;
$this->title = 'Пользователи';
$this->params['breadcrumbs'][] = $this->title;
?>
<div id="content-container">
<div id="page-title">
<h1 class="page-header text-overflow"><?= Html::encode($this->title) ?></h1>
</div>
<div id="page-content">
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="col-lg-12">
<form action="<?=\yii\helpers\Url::to(['admin/users'])?>" method="get" class="search-form" role="search">
<input id="search-field" name="q" type="text" placeholder="Искать ..." class="hint" autocomplete="off" />
<button id="search-submit" type="submit">Найти</button>
</form>
</div>
<div id="page-content">
<div class="row">
<div class="panel">
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Balance</th>
<th>Email</th>
<th>Username</th>
<th>Full Name</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<? foreach ($model as $key => $value): ?>
<tr>
<td><a class="btn btn-xs btn-success" href="<?= Url::to(['admin/user-edit', 'id' => $value->id]); ?>"><?= Yii::t('site', 'Edit'); ?></a></td>
<td><?= $value->balance; ?></td>
<td><?= $value->email; ?></td>
<td><?= $value->username; ?></td>
<td><?= $value->fullName; ?></td>
<td><?= $value->id; ?></td>
</tr>
<? endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
gii
генератором и сгенерировать контроллер, модель и представления, в том числе и поиск – Сергей Мишин 17 июл '19 в 17:15