A
A
Alexey Gus2021-12-16 13:56:37
PHP
Alexey Gus, 2021-12-16 13:56:37

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

3 answer(s)
A
Alexey Gus, 2021-12-17
@Holyboom

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',
    ],

created a class responsible for tasks
<?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);
    }
}

and the class with the task itself
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);
        }
    }
}

and made a call on save
public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ($this->status != 1){
            QueueHelper::setDelayTask(new TaskEndEvent(['idTask'=>$this->id]), 7200);
        }
    }

and cron ran a check for actual tasks
cron run * * * * * /usr/bin/php /var/www/my_project/yii queue/run

you can hang a task listener in the console
yii queue/listen

V
vilinyh, 2021-12-16
@vilinyh

- 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.

A
Andrey Pavlenko, 2015-07-14
@aaadddminnn

$fp=fopen('file.txt', 'r');
$str=fgets($fp);

You can also add a lock if necessary and close the file after reading with fclose($fp);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question