Luckatme
Luckatme

Reputation: 9

Yii2 find with ActiveRecord return simple attribute array

i have a problem using active record in Yii2. When i do:

$x = Model::find()->all();

What i expected the value of $x will be:

array(2) {
    [0] => array(2) {
        ['col1'] => string(3) "xxx2"
        ['col2'] => string(3) "xxx3"
    }
    [1] => {
        ['col1'] => string(3) "xxx2"
        ['col2'] => string(3) "xxx3"
    }
}

What i got with $x were:

array(2) { 
    [0]=> object(app\models\X)#77 (13) { 
        ['col1'] => string(3) "xxx2"
        ['col2'] => string(3) "xxx3" 
        ["_attributes":"yii\db\BaseActiveRecord":private]=> array(5) { 
            ['col1'] => string(3) "xxx2"
            ['col2'] => string(3) "xxx3"
        } 
        ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(5) { 
            ['col1'] => string(3) "xxx2"
            ['col2'] => string(3) "xxx3"
        } 
        ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } 
        ["_errors":"yii\base\Model":private]=> NULL 
        ["_validators":"yii\base\Model":private]=> NULL 
        ["_scenario":"yii\base\Model":private]=> string(7) "default" 
        ["_events":"yii\base\Component":private]=> array(0) { } 
        ["_behaviors":"yii\base\Component":private]=> array(0) { } 
    } 

    [1]=> object(app\models\X)#77 (13) { 
        ['col1'] => string(3) "xxx2"
        ['col2'] => string(3) "xxx3"
        ["_attributes":"yii\db\BaseActiveRecord":private]=> array(5) { 
            ['col1'] => string(3) "xxx2"
            ['col2'] => string(3) "xxx3"
        } 
        ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(5) { 
            ['col1'] => string(3) "xxx2"
            ['col2'] => string(3) "xxx3"
        } 
        ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } 
        ["_errors":"yii\base\Model":private]=> NULL 
        ["_validators":"yii\base\Model":private]=> NULL 
        ["_scenario":"yii\base\Model":private]=> string(7) "default" 
        ["_events":"yii\base\Component":private]=> array(0) { } 
        ["_behaviors":"yii\base\Component":private]=> array(0) { } 
    } 
}

The question is, how to make the $x's value just like what i expected. I use Yii2 basic. Thanks guys, i appreciate your help.

Upvotes: 0

Views: 1130

Answers (1)

Naim Malek
Naim Malek

Reputation: 1184

Use it as

$x = Model::find()->asArray()->all();

Upvotes: 1

Related Questions