我的数据库中有一个名为publish_on_date的字段,其中我存储了帖子应该变为活动状态的日期。
现在我不确定如何将查询添加到我的数据提供者/搜索模型。
所以我需要添加一个查询来查询显示日期并检查它是否小于或等于现在(现在是当前日期/服务器日期)。
这是我目前的搜索功能
public function search($params, $pageSize = 3, $published = false)
{
$query = Article::find();
// this means that editor is trying to see articles
// we will allow him to see published ones and drafts made by him
if ($published === true)
{
$query->where(['status' => Article::STATUS_PUBLISHED]);
$query->orWhere(['user_id' => Yii::$app->user->id]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['id' => SORT_DESC]],
'pagination' => [
'pageSize' => $pageSize,
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'status' => $this->status,
'category' => $this->category,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'summary', $this->summary])
->andFilterWhere(['like', 'content', $this->content]);
return $dataProvider;
}
答案 0 :(得分:2)
只需使用
添加其他条件$query->andWhere('publish_on_date <= NOW()');
您可以在首次创建查询时添加此项
$query = Article::find()->andWhere('publish_on_date <= NOW()');
或稍后页面