Yii Framework Forum: [Solved] "failed To Open Stream" When Adding Cache Filter To Page With Renderdynamic? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 16 November 2012 - 06:01 PM

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?
0

#2 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 16 November 2012 - 07:16 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users