Reputation: 1575
How can I return all results of yii\db\Query
as an object from a query like this one?
$query = new Query;
$query->select('id, name')
->from('user')
->limit(10);
$rows = $query->all();
Upvotes: 0
Views: 4246
Reputation: 133370
Try this way
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'];
}
for accessing via $row->name;
try with
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;
}
Upvotes: 2