2013-08-30 22 views
0

有两种模型,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, 
     ));  
} 

模型,关系,视图和创建操作似乎工作正常,我可以将数据保存到两个表中。问题是User模型中有一个字段'role',它不是从表单提供的,而是预先设置的,具体取决于传递给控制器​​动作($ role)的参数。我在创建操作设置此$角色值作为应用PARAM本身

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

和用户模型中,我使用一个函数来确定字段的在此基础上应用参数的时值的值。这里的功能,

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()中对一个值进行硬编码,它将保存/正常工作。但是否则该函数返回'空白'。这里出了什么问题?另外,我不完全确定自己是否以正确的方式做我想做的事,那么有没有更好的方法来做到这一点?

编辑:这里是配置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'=>'[email protected]', 
     'u_role'=>'', 
    ), 
); 
+0

您正在使用其他和其他 - 如果只。您是否尝试添加最后的else语句来检查前两个失败? –

+0

是的。它返回空白。也尝试了开关箱,它返回默认值。我想问题是Yii :: app() - > params ['u_role']在fixUrole()函数中返回空白。 – redGREENblue

+0

你可以在这里发布你的配置文件main.php吗? –

回答

0

我不认为你可以设置/运行期间更改PARAMS(在控制器)。

注意,此方法为静态配置参数 - 它不提供对改变的动态参数(或持续)在运行时,或者每个用户设置。

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

+0

那么,什么是实现我在这里尝试做的正确方法? – redGREENblue

+0

另外,按照这个链接,它似乎可以在运行时设置/获取参数。 http://www.yiiframework.com/wiki/242/yii-registry-how-to-use-it-does-it-exist-at-all/ – redGREENblue

+0

这是一个全球性的设置。您不应该为每个用户设置使用参数。你应该通过模型来做到这一点。 –

0

您可以尝试 的Yii ::应用程序() - >会话( 'u_role')this tutorial可以帮助你也。

相关问题