We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17544
    • 86 Posts
    I wrote a snippet that will let me edit my MODx contents directly, especially snippets and plugins (update the database directly).

    After the edit, MODx will not use the new snipper/plugin. It will still use the old snippet/plugin. I need to do a ’refresh’ by either clicking on ’clear cache’ in the manager, or edit and save anysnippet or plugin in the manager.

    I try to look at the source codes of MODx for ’clear cache’ and also saving snippet and plugin. All I can found was some lines of cache clearing code, so I add them as well to my snippet in hope that everytime I edit my snippet/plugin manually using my snippet, I do not need to go into manager and ’clear cache’.

    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
    }
    


    The above code doesn’t work for me. Am I missing anything? The above code do works well on refreshing site content but not snippet, plugin and chunks.
      • 25663 MODX Staff
      • 12,272 Posts
      Did you actually call the function in the code, or just include the function part?
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 17544
        • 86 Posts
        If you mean calling clear_cache(), yes, I did call it at immediately after I do the mysql update.

        Quite headache for me as I have been trying to figure this out for 2 months. Hopefully someone can spark me some light, I have no more clues on what to do.

          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: lovingmodx at Dec 05, 2006, 09:43 AM

          If you mean calling clear_cache(), yes, I did call it at immediately after I do the mysql update.

          Quite headache for me as I have been trying to figure this out for 2 months. Hopefully someone can spark me some light, I have no more clues on what to do.
          $modx->clearCache(); // double clear cache

          What exactly is this supposed to do? Never heard of that function on the MODx DocumentParser class... a.k.a. $modx... did you hack DocumentParser to add that function?
            • 17544
            • 86 Posts
            Quote from: OpenGeek at Dec 05, 2006, 10:18 AM

            $modx->clearCache(); 

            What exactly is this supposed to do? Never heard of that function on the MODx DocumentParser class... a.k.a. $modx... did you hack DocumentParser to add that function?

            Not hacked. But come as an original API function around line 1480

                function clearCache() {
                    $basepath= $this->config["base_path"] . "assets/cache";
                    if (@ $handle= opendir($basepath)) {
                        $filesincache= 0;
                        $deletedfilesincache= 0;
                        while (false !== ($file= readdir($handle))) {
                            if ($file != "." && $file != "..") {
                                $filesincache += 1;
                                if (preg_match("/\.pageCache/", $file)) {
                                    $deletedfilesincache += 1;
                                    unlink($basepath . "/" . $file);
                                }
                            }
                        }
                        closedir($handle);
                        return true;
                    } else {
                        return false;
                    }
                }
            


            What exactly do I need to do to refresh MODx snippet and plugin if I modify the database directly?
              • 22303 MODX Staff
              • 10,725 Posts
              Wow; I’m not sure how I’ve overlooked that function... embarrassed but anyway, it is redundant. The cache sync processor does the same plus a lot more.

              As for why it’s not working, not sure I can offer any advice without more context for the code...
                • 17544
                • 86 Posts
                It is as simple as this. $_POST[’content’] contain the new code for any snippet that I want to update. $_GET[’id’] is the id of the snippet.

                The posted snippet will be saved to the database successfully. But MODx will still use the old snippet instead of the updated. I will have to ’clear cache’ OR open a snippet in MODx manager and save it to somehow ’refresh’ MODx and force it to use the updated snippet.

                		$id = $_GET['id'];
                		$content = $modx->db->escape($_POST['content']);	
                		$fields['snippet'] = $content;
                		$modx->db->update($fields, $table_name['snippet'], "id=$id"); 
                		clear_cache();
                
                  • 22303 MODX Staff
                  • 10,725 Posts
                  $_GET[’id’] could actually be the document id, depending on friendly URL settings. And this will refresh it only for the next visitor to request the site, and certainly will not refresh the snippet within the scope of your current client request. You are not attempting to see the updated snippet within the same request scope, are you?
                    • 17544
                    • 86 Posts
                    $_GET[’id’] is actually the snippet ID (column `id` in the table `modx_site_snippets`).

                    I am using my code so I can modify snippet from the front end (instead of inside manager). I use it so I can write my PHP code locally and quickly post it up.

                    As long as I don’t click on ’refresh cache’ or save a snippet/plugin in the manager, MODx will keep on using the old (cached?) snippet/plugin, no matter how many new request I made.
                      • 17544
                      • 86 Posts
                      My research show that the api function

                      $modx->clearCache();

                      will not update the cache in assets/cache/siteCache.idx.php

                      For example if I have a snippet call [[test]]

                      and it’s code is simple

                      echo "YAYAYA";

                      siteCache.idx.php will have a line

                      $s[’Test’] = ’echo " YAYAYA";’;

                      ---------

                      If I modify the snippet manually to

                      echo "NONONO";

                      and run $modx->clearCache();

                      assets/cache/siteCache.idx.php is not updated!

                      BUT, if I use ’clear cache’ or save any snippet/plugin inside the manager, the line will change to
                      $s[’Test’] = ’echo " NONONO";’;

                      I my explanation is clearer now. I need to do what code to use in order to update the assets/cache/siteCache.idx.php