I am working on an application using yii. I have an action let acmanageappointments/index
. I have defined its rule as follow
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('index','create','update','delete','updatestatus'),
'users'=>array('@'),
and its action is as follow :
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,
));
}
This action can only be accessed with authenticated persons. when I am logged in then this function is working properly. but when I am logged out and executing this action then it gives CDbException
. How can I handle this exception, and when the user is logged out and if he is trying to access this url then he should be redirected on login page . How can I do this ?
update : Here is my 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('*'),
),
);
}
and here is the error :
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
As topher mentioned in comments, you need a filters
method.
Make sure you have this in your controller, else your access rules will do nothing:
public function filters()
{
return array(
'accessControl',
);
}
If it works, give his answer credit when he updates it with this snippet.