Gridview search dont works!

Hey, i tried to make an search for my gridview… But when i search something nothing happens.

My index site with the gridview:

<?php
use yii\grid\GridView;
use yii\data\SqlDataProvider;
use yii\widgets\Pjax;
use app\models\EmailSearch;

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT * FROM email ORDER BY `email`.`id` DESC'
]);

$searchModel = new EmailSearch();

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'subject',
        'emailTo',
        'emailFrom',
        [
            'attribute' => 'text',
            'value' => 'text'
        ],
    ]
]);

?>

my form:

<?php   
namespace app\models;   
   
use Yii;   
   
class EmailForm extends \yii\db\ActiveRecord   
{   

    public $subject;
    public $emailTo;
    public $emailFrom;
    public $text;


    /**  
     * @inheritdoc  
     */   
    public static function tableName()   
    {   
        return 'email';   
    }   
    
    public function getText() {
        return $this->text;
    }

    /**  
     * @inheritdoc  
     */   
    public function rules()   
    {   
        return [
            [['subject', 'emailTo', 'emailFrom', 'text'], 'required'],
            ['emailTo', 'email'],
        ];
    }

}  

my search:

<?php

namespace app\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\EmailForm;

/**
 * EmailSearch represents the model behind the search form of `app\models\EmailForm`.
 */
class EmailSearch extends EmailForm
{

    public $text;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['subject', 'emailTo', 'emailFrom', 'text'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EmailForm::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        $query->andFilterWhere(['like', 'subject', $this->subject])
            ->andFilterWhere(['like', 'emailTo', $this->emailTo])
            ->andFilterWhere(['like', 'emailFrom', $this->emailFrom])
            ->andFilterWhere(['like', 'text', $this->text]);

        return $dataProvider;
    }
}

What am i doing wrong?

Best regards

Hi @TimoB2005,

Did you modified the code which gii’s CRUD generator had created for you?

Try once again the original code like the following:

// $dataProvider = new SqlDataProvider([
//     'sql' => 'SELECT * FROM email ORDER BY `email`.`id` DESC'
// ]);
$searchModel = new EmailSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

EmailSearch::search() will return the data provider for the grid, constructing the query dynamically using the search parameters generated by the grid’s header columns (or the search form of the index view).

When i do that my code doesnt work at all. The Only thing that is generated from that is the EmailSearch. The other Things coded i myself.

Take some time to try the CRUD generator of Gii. It’s worth trying.

yeah i tried but nothing works. The search is the only thing

Hmm, I don’t know what’s wrong.

Anyway, the change I suggested should work.

// $dataProvider = new SqlDataProvider([
//     'sql' => 'SELECT * FROM email ORDER BY `email`.`id` DESC'
// ]);
$searchModel = new EmailSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

Or, something may be wrong with your view code.

it creates 2 “users” but nothing is set there

2 “users” ? what’s that?

Its like that. In my DB are 2 things registered but nothing is set there.

What are the expected results for “Subject”, “Email To”, “Email From” and “Text”?
Are you sure you have some valid strings stored in your “email_form” table?

yeah there is some email example stuff.

When i make it like that above everything is set correctly but the search bars dont work.

Ah, I think I got it.

You have failed to create “EmailForm” model correctly.

<?php   
namespace app\models;   
   
use Yii;   
   
class EmailForm extends \yii\db\ActiveRecord   
{   

//    public $subject;
//    public $emailTo;
//    public $emailFrom;
//    public $text;

    /**  
     * @inheritdoc  
     */   
    public static function tableName()   
    {   
        return 'email';   
    }   
    
//    public function getText() {
//        return $this->text;
//    }

    /**  
     * @inheritdoc  
     */   
    public function rules()   
    {   
        return [
            [['subject', 'emailTo', 'emailFrom', 'text'], 'required'],
            ['emailTo', 'email'],
        ];
    }
}

Do not manually declare the column names of the table as the properties. They are automatically set in the ActiveRecord base class reading the table schema.

Try the model generator of Gii at first.

It works thank you

and how can i reverse my data provider?

When i am doing

$dataProvider = array_reverse($searchModel->search(Yii::$app->request->queryParams));

I get an error.

You can not array_reverse a data provider. because it not an array. It’s an object that can provide an array of data.

And, this is very important, a grid view doesn’t accept a data array directly. It accepts a data provider which will provide a data array to the grid view.

“DISPLAYING DATA” section of the guide is a must read.

Guide > DISPLAYING DATA >