Math operate column data between 2 tables in yii2


(gebhe) #1

I tried to add some of data in different table (today_price and add_price). The data that I tried to operate is NAME and CURRENT_PRICE in Table today_price and NAME and ADD_PRICE in add_price. NAME in Table today_price is unique, but in add_price is not

public function actionIndex()
{

    $max_add = add_price::find()->select('ID')->where(['STATUS' => 'DONE'])->count();

    $max_today = today_price::find()->select('ID')->where(['BILL_DATE' => 'SEP-2019'])->count();

    $add_price = add_price::find()->select([
            'NAME',
            'ADD_PRICE'
        ])->orderBy(['NAME' => SORT_DESC])->where(['STATUS' => 'DONE'])->all();

    $add_price = ArrayHelper::toArray($add_price);

    $today_price = today_price::find()->select([
            'NAME',
            'CURRENT_PRICE'
        ])->orderBy(['NAME' => SORT_DESC])->where(['BILL_DATE' => 'SEP-2019'])->all();

    $today_price = ArrayHelper::toArray($today_price);

    for($i=0;$i<$max_add;$i++){//almost 1000 data
        for($n=0;$n<$max_today;$n++){//almost 9000 data  
            if($today_price[$n]['NAME'] == $add_price[$i]['NAME']){
                    $today_price[$n]['CURRENT_PRICE'] = 
                    $add_price[$i]['ADD_PRICE'] + 
                    $today_price[$n]['CURRENT_PRICE'];
                //$n=$n+1;
            }
        }
        //$i=$i+1;
    }

    return $this->render('index', [
        'today_price' => $today_price,
        'add_price' => $add_price
    ]);
}

But it has error

Illegal offset type in isset or empty

Anybody know to solve this problem?


*ID is how many data in database