T
T
truekenny2019-06-28 13:15:41
Yii
truekenny, 2019-06-28 13:15:41

Yii2: Why are the received values ​​of var_dump($ar['test'] ?? 0) and var_dump($ar['test']) different for ActiveRecord?

Model:

class ArModel extends yii\db\ActiveRecord
{
    public $test;
}

Usage example:
$ar = new ArModel();
$ar->test = 5;
var_dump($ar['test'] ?? 0);
var_dump($ar['test']);

Conclusion:
int(0)
int(5)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2019-06-28
@BorisKorobkov

Because the database table for this model is not specified.
$ar['test'] ?? 0is equivalent to Invoke , which catches the Exception "The table does not exist" and returns false.isset($ar['test']) ? $ar['test']: 0
There is a call \yii\db\BaseActiveRecord::__get()that returns a value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question