2012-11-17 29 views
0

我希望每次调用函数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

actionNewAnswer()调用只在一个请求到服务器一次,下一次它的新

1,添加到配置

'cache' => 'CFileCache' 

2日,在控制器类中添加常量

const myStaticCount = 'myStaticCount'; 

3,新增功能新回应

$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); 
+0

'$ count'不应该从0开始,所以要在第一次调用时转到1? –

+0

@MatteoTassinari是的,它是!但作者有一些奇怪的逻辑,也许这很重要:) – Sergey

+0

谢谢!是的编号为1的计数器是由另一种方法生成的。 –

相关问题