Yii2 仅显示发布日期小于或等于当前日期时间的数据

Yii2 only display data where posted on date is less than or equal to current datetime

我的数据库中有一个名为 publish_on_date 的字段,我在其中存储 post 应该激活的日期。

现在我不确定如何将查询添加到我的 dataprovider/search 模型中。

所以我需要添加一个查询来查询显示日期并检查它是否小于或等于现在(现在是当前 date/server 日期)。

这是我当前的搜索功能

    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;
}

只需在某个地方添加另一个条件

$query->andWhere('publish_on_date <= NOW()');

您可以在首次创建查询时添加此项

$query = Article::find()->andWhere('publish_on_date <= NOW()');

或页面下方