Certainly! Let's create a basic example of Yii2's model, search model, and view file integrating a GridView with sorting and filtering features. In this example, we use a hypothetical Post model with attributes such as id, title, created_at, and description.
Model: Post.php
First, ensure you have a basic model file generated, likely through Gii, for your database table, post.
namespace app\models;
use yii\db\ActiveRecord;
class Post extends ActiveRecord
{
public static function tableName()
{
return 'post'; // Assume your table is named 'post'
}
// Add other rules if necessary
public function rules()
{
return [
[['title', 'description'], 'string'],
[['created_at'], 'safe'],
];
}
}
Search Model: PostSearch.php
Create a search model that extends your post model to handle searching and filtering.
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class PostSearch extends Post
{
public function rules()
{
return [
[['id'], 'integer'],
[['title', 'description'], 'safe'],
[['created_at'], 'safe'],
];
}
public function search($params)
{
$query = Post::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 20],
'sort' => [
'attributes' => [
'id',
'title',
'created_at',
],
'defaultOrder' => [
'created_at' => SORT_DESC,
],
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['id' => $this->id])
->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['=', 'created_at', $this->created_at]);
return $dataProvider;
}
}
View File: index.php
Finally, in your view file, configure the GridView to use the PostSearch model.
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\PostSearch;
/* @var $this yii\web\View */
/* @var $searchModel app\models\PostSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Posts';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
[
'attribute' => 'created_at',
'format' => ['date', 'php:Y-m-d H:i:s'],
'filter' => \yii\jui\DatePicker::widget([
'model' => $searchModel,
'attribute' => 'created_at',
'dateFormat' => 'yyyy-MM-dd',
]),
],
'description:ntext',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
Optional Search Form: _search.php
This optional file provides search fields outside the GridView for user input. Yii2 can automatically generate these from the filterModel too.
use yii\widgets\ActiveForm;
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\PostSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="post-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'created_at') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Summary
- Post model: Represents your database schema structure.
- PostSearch model: Handles search and filter logic, extends the query with conditions.
- View (index.php): Combines search and display logic via GridView.
- Optional search form: Allows user input to dynamically filter GridView content.
Ensure you have DATE inputs properly set up in your actual database and adjust any attributes or rules to match your schema. This example assumes a basic setup and may require adjustments based on the specific application environment and database structure.