In the yii2 documentation
I found there is a way to convert active record to array .
Customer::find()->asArray()->all();
But I can't use like this :-
Customer::findModel($id)->asArray();
How should I do? Please help
You should add asArray()
to ActiveQuery
, not to the instance of ActiveRecord
.
Assuming your primary key column named id
, you should change your model finding code to:
Customer::find(['id' => $id])->asArray()->one();
$model = Customer::find($id)->asArray()->one();
$model = Customer::find($id)->select('id,name')->asArray()->one();
$model = Customer::find($id)->select('id,name as full')->asArray()->one();
$model = Customer::find()->where(['email'=>$email])->asArray()->one();
$model = Customer::find($id)->asArray()->all();
you may use
$model = Customer::findModel($id);
$model->attributes;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With