Yii2 findModel 到数组

Yii2 findModel to Array

yii2 documentation 我发现有一种方法可以将活动记录转换为数组。

Customer::find()->asArray()->all();

但我不能这样使用:-

Customer::findModel($id)->asArray();

我该怎么办?请帮忙

您应该将 asArray() 添加到 ActiveQuery,而不是 ActiveRecord 的实例。 假设您的主键列名为 id,您应该将模型查找代码更改为:

Customer::find(['id' => $id])->asArray()->one();

你可以使用

$model = Customer::findModel($id);
$model->attributes;

整个模型作为数组

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

Select 特定列

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

Select 特定列作为别名

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

Where条件

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

整条记录?

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