【发布时间】:2016-11-01 05:03:11
【问题描述】:
Yii2 ArrayHelper 的辅助方法 toArray 不会转换嵌套对象。
这是我的测试代码。
public function actionTest()
{
$product = \common\models\Product::find()
->where(['id' => 5779])
->with('firstImage')
->one();
$product = \yii\helpers\ArrayHelper::toArray($product);
print_r($product);
}
默认启用递归属性。
公共静态数组 toArray ( $object, $properties = [], $recursive = 真的)
所以这段代码应该可以正常工作,但它不能。
Action 返回没有firstImage 对象的一级数组。
我在这里做错了什么?
PS:
出于测试目的简化了代码。我知道在这种特定情况下,可以简单地使用asArray() 方法来获取数组中的 AR 记录。
【问题讨论】: