Yii2 gridview搜索不适用于Date字段

时间:2017-12-11 05:53:34

标签: search yii2

在我的项目中有一个Yii2 gridview。我有一个联系人表,已修改是其中的列,当我在该列中搜索日期时,它是日期字段它给出了错误

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%')

如果我替换

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

到此代码

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

如果我更改上面给出的代码,它既不会给出错误也不会给出结果.. 我不明白我哪里出错了?

1 个答案:

答案 0 :(得分:1)

试试这个

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

参考asDate()