我是 Yii 的新手,现在正在学习它。在这里,我试图从数据库的用户表中获取用户列表。
以下是我用于查看的用户控制器功能:
class UsersController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
public function actionView()
{
$model = new Users;
$this->render('view',array(
'model'=>$model,
));
}
}
以下是我的用户模型:
class Users extends CActiveRecord
{
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('fname, lname, email', 'required'),
array('fname, lname', 'length', 'max'=>50),
array('email', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, fname, lname, email', 'safe', 'on'=>'search'),
);
}
/**
* @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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'fname' => 'First Name',
'lname' => 'Last Name',
'email' => 'Email',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('fname',$this->fname,true);
$criteria->compare('lname',$this->lname,true);
$criteria->compare('email',$this->email,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
以下是我的看法:
<h1>Users</h1>
<p>
Below is the list of users, here you may add user.
</p>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'fname',
'lname',
'email',
),
)); ?>
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ? >
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('fname')); ?>:</b>
<?php echo CHtml::encode($data->fname); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('lname')); ?>:</b>
<?php echo CHtml::encode($data->lname); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('email')); ?>:</b>
<?php echo CHtml::encode($data->email); ?>
<br />
</div>
我收到一个 PHP 通知,说未定义变量:模型,请提前帮助谢谢。