0

我正在使用 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,那么他应该被重定向到 login page 。我怎样才能做到这一点 ?

更新:这是我的访问规则:

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
4

4 回答 4

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('*'),
),
于 2013-02-16T11:33:29.677 回答
1

正如评论中提到的topher,您需要一个filters方法。

确保你的控制器中有这个,否则你的访问规则不会做任何事情:

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

如果它有效,请在他使用此代码段更新它时给予他的答案。

于 2013-02-16T16:27:29.573 回答
1

您可以在配置文件“main.php”中设置 errorHandler

'components'=>array(
    ...............
    ...............
    'errorHandler'=>array(
    // use 'site/error' action to display errors
    'errorAction'=>'site/error',
    ),
    ...............
    ...............
)

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

于 2013-06-06T17:19:32.437 回答
0

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

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

当您使用此条件执行时,您会收到 SQL 错误

于 2013-02-17T06:33:18.567 回答