Goli
Goli

Reputation: 436

Yii2 gridview search is not working for Date field

In my project there is a Yii2 gridview. I have a contacts table and modified is the column in it which is date field when I search for a date in that column It gives error

Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'modified' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `contacts` LEFT JOIN `companies` ON `contacts`.`company_id` = `companies`.`id` INNER JOIN `countries` ON `contacts`.`country` = `countries`.`country_code` WHERE (`modified`='2017') AND (`modified` LIKE '%2017%')

If I replace the code from

$query->andFilterWhere([
            ...
            'modified' => $this->modified,
            ...
        ]);

to this code

$query->andFilterWhere([
            ...
            'contacts.modified' => $this->modified,
            ...
        ]);

If I change the code as given above,It neither gives error nor the result.. I cant understand where I am going wrong?

Upvotes: 0

Views: 539

Answers (1)

vishuB
vishuB

Reputation: 4261

Try this

$query->andFilterWhere([
    'DATE_FORMAT(contacts.modified, "%Y-%m-%d")' => \Yii::$app->formatter->asDate($this->modified, 'yyyy-MM-dd'),
]);

Refer asDate()

Upvotes: 1

Related Questions