q3d
q3d

Reputation: 3523

Selecting data from a database (Yii)

I've been getting in to Yii, and I would like to know how to select data from a database and loop through the rows. I've done this in CodeIgniter and I can't find anywhere which documents how I would go about doing this. I have the code:

$models = TblMess::model()->findAll(array(
'condition' => 'messid > :minid',
'params' => array(':minid' => '1'),
));

which should select all rows where the message ID is larger than one. How do I loop through all the rows selected and do something with them? eg.

foreach($rows as $row){
    echo 'Message: '.$row['message'].'. Created at: '.$row['time'].'.';
}

This seems like a simple question, but I think I'm missing something!

Upvotes: 0

Views: 4725

Answers (1)

user1233508
user1233508

Reputation:

foreach($models as $model) {
    echo 'Message: ', $model->message, '. Created at: ', $model->time, '.';
}

should work. Data queried through CActiveRecord classes is returned as objects (in this case, instances of TblMess class), not as arrays.

Upvotes: 3

Related Questions