无法从模型访问应用程序参数


Not able to access application param from model

有两个模型,User和UserProfile。保存新用户时,使用单个表单将数据保存到两个模型/表中。

public function actionCreate($role)
{
    $User = new User;
    $UserProfile = new UserProfile;
    Yii::app()->params['u_role'] = $role;
    if(isset($_POST['User'], $_POST['UserProfile']))
        {
            $User->attributes=$_POST['User'];
            $UserProfile->attributes=$_POST['UserProfile'];
            $valid=$User->validate(); 
            if($valid)  
                {
                    if($User->save(false))  
                    {
                        $UserProfile->user_id = $User->id;  
                        if ($UserProfile->save()) 
                            {
                                $model=User::model()->with('userProfiles')->findByPk($User->id);
                                $this->redirect(array('manage/list'));
                            }
                    }
                }
        }
        $this->render('create', array(
            'User'=>$User,
            'UserProfile'=>$UserProfile,
        ));     
}

模型、关系、视图和创建操作似乎运行良好,我可以将新用户的数据保存到这两个表中。问题是用户模型中有一个字段"role",它不是从表单中提供的,而是预先设置的,具体取决于传递给控制器操作($role)的参数。我将这个$role值设置为创建操作本身中的应用程序参数

Yii::app()->params['u_role'] = $role;

在User模型中,我使用了一个函数来根据这个应用程序参数的值来确定字段的值。这是的功能

public function fixUrole()
    {
        $returnUrole;
        if (Yii::app()->params['u_role']=='adm')
        {
            $returnUrole=1;
        }
        else if (Yii::app()->params['u_role']=='mgr')
        {
            $returnUrole=2;
        }
        return $returnUrole;
    }

它是从beforeValidate()调用的,如下所示。

$this->role = $this->fixUrole();

问题是,使用应用程序参数获取值时出现了问题。如果我在函数fixUrole()中硬编码一个值,它将正确保存/工作。但在其他情况下,函数返回"blank"。这里出了什么问题?此外,我不完全确定我是否以正确的方式做了我想做的事情,那么有更好的方法吗?

编辑:这是配置main.php

<?php
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'MY APP NAME',
    // preloading 'log' component
    'preload'=>array(
        'log',
        'bootstrap'),
    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),
    'modules'=>array(
        // uncomment the following to enable the Gii tool
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'enter',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
            'generatorPaths' => array(
            'bootstrap.gii'
        ),
        ),/**/
    ),
    // application components
    'components'=>array(
        'user'=>array(
            //'allowAutoLogin'=>true,
              'class' => 'WebUser',
        ),
        'bootstrap' => array(
        'class' => 'ext.bootstrap.components.Bootstrap',
        'responsiveCss' => true,
        ),
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=testdb1',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ),
        'errorHandler'=>array(
            // use 'site/error' action to display errors
            'errorAction'=>'site/error',
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
                // uncomment the following to show log messages on web pages
                array(
                    'class'=>'CWebLogRoute',
                ),
                /**/
            ),
        ),
    ),
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
        // this is used in contact page
        'adminEmail'=>'webmaster@example.com',
         'u_role'=>'', 
    ),
);

我认为您不能在运行时(在控制器中)设置/更改参数。

请注意,此方法适用于静态配置参数-它的不提供在运行时更改(或持久化)的动态参数

请检查这篇文章的正确用法:http://www.yiiframework.com/wiki/126/setting-and-getting-systemwide-static-parameters/

您可以尝试Yii::app()->会话('u_role')本教程也可能对您有所帮助。