将从数据库查找的对象,转换成数组,并且以设定属性键名称,用到ArrayHelper::toArray
$posts = Post::find()->limit(10)->all();
$data = ArrayHelper::toArray($posts, [
'appmodelsPost' => [
'id',
'title',
// the key name in array result => property name
'createTime' => 'created_at',
// the key name in array result => anonymous function
'length' => function ($post) {
return strlen($post->content);
},
],
]);