yii 未定义的变量:模型化

qgzx9mmu  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(120)

我是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通知,说未定义的变量:模特,请帮忙,先谢谢了。

cedebl8k

cedebl8k1#

看起来像是在粘贴_view.php。请确保将$model变量从view.php传递到_view.php,因为在actionView()中,您只将它传递给'view'。

falq053o

falq053o2#

文本输入(('自动聚焦((〉真,(必需' =〉真])?〉文本输入(['自动聚焦' =〉真,'必需' =〉真])?〉文本输入(['自动聚焦' =〉真,'必需' =〉真])?〉

相关问题