2013-02-16 135 views
0

我正在使用yii处理应用程序。我有一个行动让acmanageappointments/index。我已经确定了它的规则如下如何处理yii中的错误

array('allow', // allow authenticated user to perform 'create' and 'update' actions 
       'actions'=>array('index','create','update','delete','updatestatus'), 
       'users'=>array('@'), 

其行动情况如下:

public function actionIndex() 
    { 

     $user_id = Yii::app()->user->getId(); 
     $criteria = new CDbCriteria(); 
     $criteria->condition = 'user_id='.$user_id; 
     $count=AcAppointments::model()->count($criteria); 
     $pages=new CPagination($count); 

     //results per page 
     $pages->pageSize=10; 
     $pages->applyLimit($criteria); 
     $AllAppointments = AcAppointments::model()->findAll($criteria); 

     // Applying Global Date Time Format 
     $condition = array('user_id' => $user_id); 
     $DTFormat = CalendarSettings::model()->findByAttributes($condition); 

     $this->render('index',array(
       'AllAppointments' => $AllAppointments, 
       'pages' => $pages, 
       'DTFormat' => $DTFormat, 
     )); 


    } 

这个动作只能通过验证的人员访问。当我登录时,此功能正常工作。但是当我注销并执行此操作时,它会提供CDbException。我该如何处理这个异常,当用户注销时,如果他试图访问这个URL,那么他应该在登录页面上重定向。我怎样才能做到这一点 ?

更新:这是我的accessrules:

public function accessRules() 
    { 
     return array(
      array('allow', // allow authenticated user to perform 'create' and 'update' actions 
       'actions'=>array('index','create','update','delete','updatestatus'), 
       'users'=>array('@'), 
      ), 
      array('deny', // deny all users 
       'users'=>array('*'), 
      ), 
     ); 
    } 

,这里是错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 
+0

您定义了哪些其他访问规则? – topher 2013-02-16 11:30:02

+0

不,我还没有定义任何其他规则 – 2013-02-16 11:30:32

回答

1

作为评论中提到的顶尖,你需要一个filters方法。

确保你在你的控制器都这样了,否则你的访问规则不会做任何事情:

public function filters() 
{ 
    return array(
     'accessControl', 
    ); 
} 

如果一切正常,给他的答案信贷时,他在这个片段中对其进行更新。

1

您需要定义另一个规则,这将确保非认证的用户被拒绝访问。这必须是最后的规则。

array('allow', // allow authenticated user to perform 'create' and 'update' actions 
    'actions'=>array('index','create','update','delete','updatestatus'), 
    'users'=>array('@'), 
), 
array('deny', 
    'users'=>array('*'), 
), 
+0

thnx topher,我也这样做了,现在我得到同样的问题 – 2013-02-16 11:34:37

+0

冲浪时,我看到我必须从index.php删除yii_debug,什么是准确 – 2013-02-16 11:35:32

+1

你可以发布你的' accessRules'方法? – topher 2013-02-16 11:36:39

0

检查值$ user_id。如果您还没有登录,你会得到空的$ user_id的

$user_id = Yii::app()->user->getId(); 
$criteria = new CDbCriteria(); 
$criteria->condition = 'user_id='.$user_id; 

当你用这个标准你有SQL错误

1

您可以在您的配置文件“main.php”

设置ErrorHandler设置执行
'components'=>array(
    ............... 
    ............... 
    'errorHandler'=>array(
    // use 'site/error' action to display errors 
    'errorAction'=>'site/error', 
    ), 
    ............... 
    ............... 
) 

在这种情况下,这会将所有异常重定向到提供的URL站点/错误。

+0

感谢ankur ji bt,我忘记的是Willem Renzema解释的那个...... – 2013-06-07 06:13:00