Yii得到缓存对象自动加载CTimestampBehavior

时间:2013-12-21 13:15:17

标签: php caching yii

如何缓存模型实例或模型数组(包括'CTimestampBehavior')并在Yii中正确检索它? 我需要在console命令中设置模型的缓存数组,并在web-app中获取它们。它在控制台中检索得很好,但我在网络应用程序中收到此警告:

include(CTimestampBehavior.php):无法打开流:没有这样的文件或目录

仅适用于具有“CTimestampBehavior”行为的模型。

它的痕迹;

2013/12/21 12:56:59 [error] [php] include(CTimestampBehavior.php): failed to open stream: No such file or directory (C:\xampp\htdocs\yii\framework\YiiBase.php:427)
Stack trace:
#0 unknown(0): spl_autoload_call()
#1 C:\xampp\htdocs\yii\framework\caching\CCache.php(108): unserialize()
#2 C:\xampp\htdocs\inews\themes\news\views\site\index.php(3): CFileCache->get()
#3 C:\xampp\htdocs\yii\framework\web\CBaseController.php(126): require()
#4 C:\xampp\htdocs\yii\framework\web\CBaseController.php(95): SiteController->renderInternal()
#5 C:\xampp\htdocs\yii\framework\web\CController.php(869): SiteController->renderFile()
#6 C:\xampp\htdocs\yii\framework\web\CController.php(782): SiteController->renderPartial()
#7 C:\xampp\htdocs\inews\protected\controllers\SiteController.php(40): SiteController->render()
#8 C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(49): SiteController->actionIndex()
#9 C:\xampp\htdocs\yii\framework\web\CController.php(308): CInlineAction->runWithParams()
#10 C:\xampp\htdocs\yii\framework\web\CController.php(286): SiteController->runAction()
#11 C:\xampp\htdocs\yii\framework\web\CController.php(265): SiteController->runActionWithFilters()
#12 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(282): SiteController->run()
#13 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(141): CWebApplication->runController()
#14 C:\xampp\htdocs\yii\framework\base\CApplication.php(180): CWebApplication->processRequest()
#15 C:\xampp\htdocs\inews\index.php(13): CWebApplication->run()
REQUEST_URI=/inews/site/index
in C:\xampp\htdocs\inews\themes\news\views\site\index.php (3)
in C:\xampp\htdocs\inews\protected\controllers\SiteController.php (40)
in C:\xampp\htdocs\inews\index.php (13)

控制台功能:

public function actionZe() {
        $timeLimit = date('Y-m-d H-i-s', strtotime('-1 month'));
        $articles = Article::model()->limit(6)->order('view_counter DESC')->findAll('Status="OK" AND CreationDate >  "'.$timeLimit.'"');

        Yii::app()->cache->set('Hot_Articles', $articles, 120);
        $value = Yii::app()->cache->get('Hot_Articles');//WORKS
        print_r($value);
    }

WebApp:

    $value = Yii::app()->cache->get('Hot_Articles');//Doesn't Work
    print_r($value );
    die();

1 个答案:

答案 0 :(得分:2)

您需要导入该文件。

Yii::import('zii.behaviors.CTimestampBehavior');

或在您的配置文件中

'import' => array(
    ....
    'zii.behaviors.CTimestampBehavior' // or 'zii.behaviors.*'
    ....
)