gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:33 31 4
gpt4 key购买 nike

我的数据库中有一个名为 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;
}

最佳答案

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

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

您可以在第一次创建查询时添加它

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

或页面下方

关于php - Yii2 仅显示发布日期小于或等于当前日期时间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30178484/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com