0

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.

2
  • Use $x = Model::find()->asArray()->all(); Commented Nov 23, 2017 at 4:31
  • Thanks @InsaneSkull it worked. Commented Nov 23, 2017 at 5:39

1 Answer 1

1

Use it as

$x = Model::find()->asArray()->all();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.