如何将yii\db\Query
的所有结果作为对象从此类查询中返回?
$query = new Query;
$query->select('id, name')
->from('user')
->limit(10);
$rows = $query->all();
答案 0 :(得分:2)
试试这种方式
use yii\db\Query;
$query = new Query;
// compose the query
$query->select('id, name')
->from('user')
->limit(10);
// build and execute the query
$rows = $query->all();
// accessing the value
foreach ($rows as $row){
echo $row['name'];
}
通过$ row->名称进行访问;
尝试
use common\models\User; // or you app or backend depend where you have models
$rows = User::find()->limit(10)->all();
foreach ($rows as $row){
echo $row->nome;
}