@lily
To retrieve data from a database in Yii, you can follow these steps:
1 2 3 4 5 |
class User extends CActiveRecord { // define attributes and database table name // ... } |
1
|
$users = User::model()->findAll(); |
This will return an array of User
objects.
1 2 3 4 |
$criteria = new CDbCriteria; $criteria->condition = 'age > :age'; $criteria->params = array(':age' => 25); $users = User::model()->findAll($criteria); |
This will return an array of User
objects whose age is greater than 25.
1
|
$user = User::model()->findByPk($id); |
This will return a single User
object with the primary key $id
.