[Solved] "failed To Open Stream" When Adding Cache Filter To Page With Renderdynamic?

On my home page I’m displaying a random item. For example:

My controller looks like:




class SiteController extends Controller

{

    public function filters()

    {

        $cacheDurationInSeconds = 604800; // seconds in a week

        

        return array(

            array(

            'COutputCache + index',

            'requestTypes' => array('GET'),

            'duration'=> $cacheDurationInSeconds,

            'dependency'=>array(

                'class'=>'system.caching.dependencies.CDbCacheDependency',

                'sql'=>'SELECT AVG(modified) FROM post',

                 ),

            ),

        );

    }

    

	public function actionIndex()

	{

            $randomPost = Post::model()->find(array('order' => new CDbExpression('RAND()')));

	    $this->render('index', array( 'randomPost' => $randomPost));

	}

...



In my index.php view:




<h3>Random Post:</h3>


<?php $this->renderDynamic('renderPartial', '/post/_view', array('data' => $randomPost), true); ?>



And the partial view:




<div class="view">

    <div class="post-index-item">

        <div class="title"><?php echo CHtml::link(CHtml::encode($data->title), array('post/view', 'slug'=>$data->slug)); ?></div>

        <div class="date">Date: <?php echo CHtml::encode($data->date); ?></div>

    </div>

</div>



If I comment out the filter method in the SiteController everything works fine (of course the caching is not implemented though), but with the filter method in place I get the following error:




Error 500

include(SluggableBehavior.php): failed to open stream: No such file or directory



(Yes the post model does use the SluggableBehavior extension.)

Why is this happening and how can I fix it?

The problem seems to have been that in my index.php view I needed to do:


<?php $this->renderDynamic('getRandomPostContent'); ?>

instead of:


<?php $this->renderDynamic('renderPartial', '/post/_view', array('data' => $randomPost), true); ?>

Then I need to create the callback function in my SiteController:


public function getRandomPostContent()

{

    $randomPost = Post::model()->find(array('order' => new CDbExpression('RAND()')));

    return $this->renderPartial('/post/_view', array('data' => $randomPost), true);

}



This makes sense because the $randomPost object is in the scope of the cached file so even if it could work I wouldn’t have the desired result because I need the object to be changed on every request.

Also I would add that the problem was compounded by the cache itself and clearing the runtime/cache folder helped.