计数器在函数global / static中

时间:2012-11-17 15:23:44

标签: php static yii

我希望每当我调用函数actionNewAnswer时,计数器就会升起。 但现在,柜台总是2.什么错了?

public function actionNewAnswer(){     
    static $count = 1;
    ++$count;

    $modelAntwoorden = $this->loadModelAntwoorden();

    $this->renderPartial('_formAnswer', array(
        'model' => new VraagAntwoord(), 
        'counter' => $count,
        'modelAntwoorden'=>$modelAntwoorden,
    ),false,true);  
}

1 个答案:

答案 0 :(得分:1)

actionNewAnswer()只在一次请求服务器时调用一次,下一次是新服务

1,添加到配置

'cache' => 'CFileCache'

2,在Controller类中添加const

const myStaticCount = 'myStaticCount';

3,添加到actionNewAnswer

$count = 0; // comment for zero +1
if (Yii::app()->cache->offsetExists(self::myStaticCount)) {
    $count = Yii::app()->cache->get(self::myStaticCount);
}
Yii::app()->cache->set(self::myStaticCount, ++$count);