如何在yii中获取表字段?

时间:2014-10-07 11:58:20

标签: php yii

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

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

<?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');?>

但是当i $ role = new role();用户角色表获取错误

 <?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($role, 'role_name');?>
    <?php echo $form->textField($role, 'role_name', $role->getHtmlOptions('role_name')); ?>
    <?php echo $form->error($role, 'role_name');?>

Plz帮帮我......

0 个答案:

没有答案