实例中使用到的表结构如下:
Model:Users.php
<?php
/**
* The followings are the available columns in table 'tbl_user':
* @property integer $id
* @property string $name
* @property integer $sex
* @property string $home
*/
class Users extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return static the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{users}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, sex, home', 'required'),
array('name', 'length', 'max'=>32),
array('home', 'length', 'max'=>64),
array('id','safe'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'name' => 'name',
'sex' => 'sex',
'home' => 'home',
);
}
}
<