Answer the question
In order to leave comments, you need to log in
How to change fields in model after n-times in Yii2?
The bottom line is, there is a model with fields: creation date, completion date and status.
the creation date is filled in on creation, and the due date must be filled in when the status changes or 2 hours after creation and then the status changes.
if everything is clear with the status change, then how to track that 2 hours have passed and fill in these fields?
there is an option in afterfind to check the dates, and change them if necessary, but how then to exclude the changed from the issue?
public function afterFind()
{
parent::afterFind();
if ($this->creation_at + 7200 < time() && $this->status != 1 ){
$this->ended_at = $this->creation_at + 7200 ;
$this->status = 1 ;
$this->save(false);
}
}
Answer the question
In order to leave comments, you need to log in
in short, how did you do it.
put plugin for yii "composer require --prefer-dist yiisoft/yii2-queue"
in common/config/main added
'components' => [
'queue' => [
'class' => \yii\queue\file\Queue::class,
'path' => '@console/runtime/queue',
'as log' => \yii\queue\LogBehavior::class,
],
'bootstrap' => [
'queue',
],
<?php
class QueueHelper
{
public static function setTask ($task) {
Yii::$app->queue->push($task);
}
public static function setDelayTask ($task , $delay = 5) { // delay - задержка выполнения в сек
Yii::$app->queue->delay($delay)->push($task);
}
}
class TaskEndEvent extends BaseObject implements \yii\queue\JobInterface
{
public $idTask;
public function execute($queue)
{
$e = Events::findOne($this->idTask);
if ($e->status != 1 ){
$e->ended_at = $e->creation_at + 7200 ;
$e->status = 1 ;
$e->save(false);
}
}
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
if ($this->status != 1){
QueueHelper::setDelayTask(new TaskEndEvent(['idTask'=>$this->id]), 7200);
}
}
cron run * * * * * /usr/bin/php /var/www/my_project/yii queue/run
yii queue/listen
- throw an update task into the queue, delayed for 2 hours,
- update the data in the database before searching for all models (for example, in the init model),
- well, and the classic - cron.
$fp=fopen('file.txt', 'r');
$str=fgets($fp);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question