我正在使用yii开发应用程序。我有一个动作acmanageappointments/index
。我已将其规则定义如下
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时,应该在登录页面上重定向。我怎样才能做到这一点 ?
更新:这是我的访问规则:
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 :(得分: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('*'),
),
答案 1 :(得分:1)
正如评论中提到的那样,您需要filters
方法。
确保您的控制器中有此功能,否则您的访问规则将无效:
public function filters()
{
return array(
'accessControl',
);
}
如果有效,请在使用此片段更新时给出答案。
答案 2 :(得分:1)
您可以在配置文件“main.php”
中设置errorHandler'components'=>array(
...............
...............
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
...............
...............
)
在这种情况下,这会将所有例外重定向到提供的URL站点/错误。
答案 3 :(得分:0)
检查值$ user_id。如果您尚未登录,则会获得空的$ user_id
$user_id = Yii::app()->user->getId();
$criteria = new CDbCriteria();
$criteria->condition = 'user_id='.$user_id;
使用此条件执行时会出现SQL错误