I
I
Iossarian2019-04-15 10:53:20
Yii
Iossarian, 2019-04-15 10:53:20

Why is cron not going through foreach?

There is a code for generating acts. Acts are generated for users with a specific role. If you run the script from the site, then everything works out, but if you run it through cron, then foreach works for the first ID and stops working. With what it can be connected? Or is it possible to somehow display the error logs from the cron? There are no errors in var/log/syslog. The action code

public function actionAdd()
    {
        //Получение кураторов
        $query = $query = new Query();
        $curators = $query->select(['user_id'])
            ->from('auth_assignment')
            ->where(['item_name' => 'curator'])
            ->distinct()
            ->all();
        //Получение стоимости полюсов

        foreach ($curators as $curator) {
            $model = PaymentInfo::find()->where(['user_id' => $curator['user_id']])->one();
            if (isset($model)) {

                $policies = PolicyPayed::find()->where(['id_seller' => $curator['user_id']])
                    ->andWhere(['between', 'date_payed', date('Y-m-d H:i:s', strtotime('-1 week')), date('Y-m-d H:i:s', strtotime('-1 day'))])->all();
                if (isset($policies)) {
                    $amount = round(array_sum(array_column($policies, 'cost')) / 100 * 9, 2);
                    //Получение последней записи для заполнения поля 'Number'
                    $id = Act::find()->max('id');
                    $last = Act::find()->where(['id' => $id])->one();

                    $act = new Act();
                    $act->user_id = $curator['user_id'];
                    $act->name = $model->name;
                    $act->inn = $model->inn;
                    $act->kpp = $model->kpp;
                    $act->legal_address = $model->legal_address;
                    $act->date_begin = date('Y-m-d H:i:s', strtotime('-1 week'));
                    $act->date_end = date('Y-m-d H:i:s', strtotime('-1 day'));
                    $act->payment_id = $model->id;
                    $act->amount = $amount;
                    (isset($last->number)) ? $act->number = $last->number + 1 : $act->number = 25;

                    $act->save();
                }
            }
        }

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-04-15
@Iossarian

Or is it possible to somehow display the error logs from the cron?

For example:
Yii::info(count($curators),'my_info_category');
....
if(!$act->save()){
   Yii::error(Json::encode($act->firstErrors),'my_error_category');
}

offtopic:
here it is
$model = PaymentInfo::find()->where(['user_id' => $curator['user_id']])->one();
            if (isset($model)) 
{

can be written like this
if($model = PaymentInfo::find()->where(['user_id' => $curator['user_id']])->one())
{

But why not choose PaymentInfo via a eager-loading link. At you so at each pass of a cycle there is a request in a DB.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question