Undefined offset: 0 in yii2

9.2k views Asked by At

I just have that code in my model, and as a result of that, I got Undefined offset: 0 what should I do to solve that? I've tried declare new variable, but still nothing change. Thank you

public function sendSMS() {
    $model2 = SibuStudent::find()->innerJoin('Sibu_Payment', 'Sibu_Payment.virtual_id=Sibu_Student.virtual_id' )->where('Sibu_Student.phone1 != "NULL" '  ) ->all();
    $model3 = SibuStudent::find()->innerJoin('Sibu_Payment', 'Sibu_Payment.virtual_id=Sibu_Student.virtual_id' )->where('Sibu_Student.student_name != "NULL" '  ) ->all();
    $model4 = SibuPayment::find()->where('Sibu_Payment.total_payment != "NULL" '  )->all();
    $model5 = SibuPayment::find()->where('Sibu_Payment.sms_status != "NULL" ')->all();
    $count = sizeof($model2);

    for($a=0; $a<5; $a++){
        if ($model5[$a]->sms_status == 0) {

        $no = $model2[$a]->phone1;
        $message = 'Kepada Bapak/Ibu dari '.$model3[$a]["student_name"].',tagihan (SPP,Asrama,Kantin,Adm) Rp.'.$model4[$a]["total_payment"].' dibayarkan sesuai tagihan. SMS ini tidak untuk dibalas, jika ada yang kurang jelas diberitahukan kepada siswa dan menanyakan kepada kami. Salam';

        if (strlen($message) < 160)
        {
            $outbox = new Outbox;
            $outbox->CreatorID = 'Gammu';
            $outbox->TextDecoded = $message;
            $outbox->DestinationNumber = $no;
            $outbox->save();
        }
    //batas for
    }
}
1

There are 1 answers

2
A l w a y s S u n n y On BEST ANSWER

You have to try this way to find data from MODELs for NOT NULL clause. Do it for all your model that checks not null condition. Because your model has no element with key 0 in this case that why it returns Undefined offset 0

SibuPayment::find()->where(['not', ['Sibu_Payment.total_payment' => null]])->all();

For example ['not', ['attribute' => null]] will result in the condition NOT (attribute IS NULL)

Check More: http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail