问题描述:

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();
网友答案:

Whole model as array

$model = Customer::find($id)->asArray()->one();

Select specific columns

 $model = Customer::find($id)->select('id,name')->asArray()->one();

Select specific columns as alias

$model = Customer::find($id)->select('id,name as full')->asArray()->one();

Where condition

$model = Customer::find()->where(['email'=>$email])->asArray()->one();

Whole records?

$model = Customer::find($id)->asArray()->all();
网友答案:

you may use

$model = Customer::findModel($id);
$model->attributes;
相关阅读:
Top