2016-08-01 30 views
0

我有下面的代碼:yii2主動查詢請求得到吸氣的值(使用asArray())

$fixed_events = EventMain::find() 
     ->select(["id", "title", "files"]) 
     //->joinWith(['files']) 
     //->with(['files']) 
     ->asArray() 
     ->all(); 

我怎樣才能陣列「文件」的價值,同時考慮到,該「文件」是MODLE的getter像

public function getFiles() 
{ 
    return (json_decode($this->all_files, true)) ?: []; 
} 
+0

如果從查詢中刪除'asArray()',並在像foreach($ fixed_events as $ event){print_r($ event-> files); }'這樣你可以訪問每個模型對象的'files array'。在你的情況下會這麼好嗎? –

回答

0

由於files不與EventMain表的關係,我想最簡單的方法是處理數據,並與ArrayHelper轉換從數據庫來後:

<?php 
use yii\helpers\ArrayHelper; 


$models = EventMain::find()->select(['id', 'title'])->all(); 

$array = ArrayHelper::toArray($models, [ 
    'app\models\EventMain' => ['id', 'title','files'] 
]); 

var_dump($array); 
?>