Yii2 how to create a publish/active on date like wordpress

I was wondering if someone could give me direction on creating a post on date where by I’d set the date within the create/update form

And then when that day comes the post is active/displayed to users.

Any direction would be appreciated, I’ll post progress here so other could use it.

Well there are a few things to this.

Please note I am assuming you are using MySQL

You would need the following in your database

display_start

status

optional*

display_end

I added status because wordpress has statuses like "published, draft, etc"

Wherever your query is for your data provider (default Yii Crud is in the search model so maybe PostSearch in your case) you could change it to something like the following.




 public function search($params) {

//to show any post that are active and set to display now (date time) or before now (date time)

    	$query = Post::find()->where('status=:status AND display_start  <=:now', [':status' => 1, ':now' => date("Y-m-d h:m:s")]);

//to show any post that are active and set to display now (date time) or before now (date time) and where a display_end is not greator than or equal to now (date time) uncomment the next line and comment the previous line to use this

    	//$query = Post::find()->where('status=:status AND display_start  <=:now and display_end >=:now', [':status' => 1, ':now'  => date("Y-m-d h:m:s")]);

    	$dataProvider = new ActiveDataProvider([

        	'query' => $query,

        	'sort' => [

            	'defaultOrder' => [

                	' display_start=> SORT_DESC,

            	]

        	]

    	]);



So now when people view or search the post it will only return results for active post

Awesome, that looks like a good solution for me. Should I be storing in an integar db field or datetime?

What’s the bestway to store is so that it can be compared to now()?

Thanks

the above example was for datetime, however, the best is all relative to your data set and use. I would start with datetime and then reevaluate after some usage.

I have the date stored in my table but having trouble implementing the query, this is my current search


    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;

    }

try adding


'query'=>$query->all()