2013-02-16 137 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