Php 如何在yii中获取表字段?

Php 如何在yii中获取表字段?,php,yii,Php,Yii,我正在为用户角色开发一个新的控制器 当我调用$user=newuser()时;对于用户表而不是表单字段值显示 <?php public function actionCreate() { $request = Yii::app()->request; $notify = Yii::app()->notify; $user = new User(); if (

我正在为用户角色开发一个新的控制器

当我调用$user=newuser()时;对于用户表而不是表单字段值显示

<?php
    public function actionCreate()
        {
            $request = Yii::app()->request;
            $notify = Yii::app()->notify;
            $user = new User();

            if ($request->isPostRequest && ($attributes = (array)$request->getPost($user->modelName, array()))) {
                $user->attributes = $attributes;
                if (!$user->save()) {
                    $notify->addError(Yii::t('app', 'Your form has a few errors, please fix them and try again!'));
                } else {
                    $notify->addSuccess(Yii::t('app', 'Your form has been successfully saved!'));
                }

                Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
                    'controller' => $this,
                    'success'    => $notify->hasSuccess,
                    'user'       => $user,
                )));

                if ($collection->success) {
                    $this->redirect(array('users/index'));
                }
            }

            $this->setData(array(
                'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t('users', 'Create new user'), 
                'pageHeading'       => Yii::t('users', 'Create new user'),
                'pageBreadcrumbs'   => array(
                    Yii::t('users', 'Users') => $this->createUrl('users/index'),
                    Yii::t('app', 'Create new'),
                )
            ));

            $this->render('form', compact('user'));
        }
        ?>

显示形式

 <?php echo $form->labelEx($user , 'first_name');?>
    <?php echo $form->textField($user , 'first_name', $role->getHtmlOptions('first_name')); ?>
    <?php echo $form->error($user , 'first_name');?>
 <?php echo $form->labelEx($role, 'role_name');?>
    <?php echo $form->textField($role, 'role_name', $role->getHtmlOptions('role_name')); ?>
    <?php echo $form->error($role, 'role_name');?>

但当i$role=newrole()时;获取用户角色表时出错

 <?php
    public function actionCreate()
    {
        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        $role = new Role();



        if ($request->isPostRequest && ($attributes = (array)$request->getPost($role->modelName, array()))) {
            $role->attributes = $attributes;
            if (!$role->save()) {
                $notify->addError(Yii::t('app', 'Your form has a few errors, please fix them and try again!'));
            } else {
                $notify->addSuccess(Yii::t('app', 'Your form has been successfully saved!'));
            }

            Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
                'controller' => $this,
                'success'    => $notify->hasSuccess,
                'user'       => $user,
            )));

            if ($collection->success) {
                $this->redirect(array('users_role/index'));
            }
        }

        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t('roles', 'Create new role'), 
            'pageHeading'       => Yii::t('roles', 'Create new role'),
            'pageBreadcrumbs'   => array(
                Yii::t('roles', 'Roles') => $this->createUrl('users_role/index'),
                Yii::t('app', 'Create new role'),
            )
        ));

        $this->render('form', compact('role'));
    }
?>

不显示表单

 <?php echo $form->labelEx($user , 'first_name');?>
    <?php echo $form->textField($user , 'first_name', $role->getHtmlOptions('first_name')); ?>
    <?php echo $form->error($user , 'first_name');?>
 <?php echo $form->labelEx($role, 'role_name');?>
    <?php echo $form->textField($role, 'role_name', $role->getHtmlOptions('role_name')); ?>
    <?php echo $form->error($role, 'role_name');?>


请帮助我..

请显示类Role()和User()的代码您遇到了什么错误?请尝试更改$Role=new Role();to$role=新角色();。确保两个模型类文件位于同一目录中。如果是的话,它一定在工作。。