We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39062
    • 6 Posts
    Hello guys,
    I made a code to save the comments given by user and show them in front end, that's running totally fine, and also showing total number of comments given in a topic (actually I am making a blog) but as user save any comment total number of comments is not updating because its using old snippet code not the updated one, once I clear the cache or save the snippet from manager total comments shows fine. So I thought to put the code which will clear the cache after submitting comment and I put that but its not working, I mean as I post the comment my page goes blank, I don't know what wrong I am doing. Below is my code:

    <?php
    	function clear_cache()
    	{
    		global $modx;
    		include_once($modx->config['base_url'] . "manager/processors/cache_sync.class.processor.php");
    		$sync = new synccache();
    		$sync->setCachepath($modx->config('base_url') . "assets/cache/");
    		$sync->setReport(false);
    		$sync->emptyCache();
    		$modx->clearCache(); // double clear cache
    	}
    	
    	$path = MODX_CORE_PATH . 'components/blog_comment/';
    	$result = $modx->addPackage('blog_comment',$path . 'model/','da_');
    	if (! $result) {
    	  return 'failed to add package';
    	} else {
    	  $output = '<p>added Package</p>';
    	}
    	/* CreateQuote Snippet */
    	
    	if (isset($_POST['submitbtn'])) {
    		/* Form has been submitted */
    		/* You might want to check for empty required fields here */
    		$blog_comment = $modx->newObject('BlogComment');
    
    		/* the next three lines can be in any order */
    		$blog_comment->set('resource_id', $_POST['blog_id']);
    		$blog_comment->set('commenter_name', $_POST['commenter_name']);
    		$blog_comment->set('commenter_email', $_POST['commenter_email']);
    		$blog_comment->set('comment_text', $_POST['commenter_text']);
    
    		$blog_comment->save();
    		$modx->clear_cache();
    	}
    	
    	/* Not a repost, just display the form */
    	if (isset($_POST['submitbtn'])) 
    		$output_form = '<span class="success_message">Comment inserted successfully</span>';
    	else
    		$output_form .= "";
    	$output_form .= $modx->getChunk('comment_area');
    	$current_blog = $modx->resource->get('id');
    	$comments = $modx->getCollection('BlogComment', array('comment_status'=>'1', 'resource_id'=>$current_blog));
    	$total_comments = count($comments);
    	$output_comment = "";
    	
    	if($total_comments)
    	{
    		$output_comment .= '<ul class="blog_comment_list">';
    			foreach($comments as $comment) {
    				$fields = $comment->toArray();
    				$fields['comment_date'] = date("dS F, Y", strtotime($fields['comment_date']));
    				$output_comment .= $modx->getChunk('comment_list', $fields);
    			}
    		$output_comment .= '</ul>';
    	}
    	else
    		$output_comment .= '<span class="no_comment_msg">Be the first to comment</span>';
    	$output = $output_form.$output_comment;
    
    	return $output;


    Please help.
      • 4172
      • 5,888 Posts
      you are calling
      $modx->clear_cache();


      but clear_cache isn't a modx-function. To call your function just do

      clear_cache();
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 39062
        • 6 Posts
        @Bruno17 I tried that also but no luck sad, even I tried to put the function code directly but that also not worked.
          • 22303 MODX Staff
          • 10,725 Posts
          You are mixing Evo's clear cache code with Revo. You need to use the modCacheManager->refresh() method in MODX 2.1+, e.g.

          <?php
          $modx->cacheManager->refresh();
          
            • 39062
            • 6 Posts
            @opengeek Thank you so much, that solved my problem smiley