We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53713
    • 3 Posts
    We have developed an e-shop and when from the web context a WebAdmin STORES a new product (resource) the [[~{id}]] tag and the makeUrl({id}) in php returns null until you save a resource or an element in the manager or until you clear the cache or regenerate the uris.

    the code as follows:
    ....

    function transliterateString($txt) {
    $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja', 'Α' => 'A', 'Ά' => 'A', 'Β' => 'V', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Έ' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Ή' => 'H', 'Θ' => 'Th', 'Ι' => 'I', 'Ί' => 'I', 'Ϊ' => 'I', 'ΐ' => 'i', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => 'X', 'Ο' => 'O', 'Ό' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Ύ' => 'Y', 'Ϋ' => 'Y', 'ΰ' => 'u', 'Φ' => 'F', 'Χ' => 'Ch', 'Ψ' => 'Ps', 'Ω' => 'O', 'Ώ' => 'O', 'α' => 'a',
    'ά' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'έ' => 'e', 'ζ' => 'z', 'η' => 'h', 'ή' => 'h', 'θ' => 'th', 'ι' => 'i', 'ί' => 'i', 'ϊ' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 'ο' => 'o', 'ό' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'ύ' => 'u', 'ϋ' => 'u', 'φ' => 'f', 'χ' => 'ch', 'ψ' => 'ps', 'ω' => 'o', 'ώ' => 'o', 'ς' => 's');
    return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
    }

    $query = $modx->newQuery("Products");
    $query->sortby("mpn", "DESC");

    $lastprod = $modx->getObject("Products", $query);

    $newmpn = $lastprod->get("mpn") + 1;

    $category = $modx->getObject("Categories", array("resource_id" => $_POST["category"]));


    $newprod = $modx->newObject("Products");
    $newprod->set("mpn", $newmpn);
    $newprod->set("title", $_POST["title"]);
    $newprod->set("resource_parent", $_POST["category"]);
    $newprod->set("category_guid", $category->get("guid"));
    $newprod->set("active", 0);
    $newprod->set("skroutz", 0);

    $newprod->save();


    $newprod_res = $modx->newObject("modDocument");



    $title = $_POST["title"];
    $title_en = transliterateString($title);

    $alias = $newprod_res->cleanAlias($title_en);

    $i=1;
    $countalias = $modx->getCount("modDocument", array("alias" => $alias));

    while($countalias>0){

    $final_alias = $title_en." ".$i;
    $alias = $newprod_res->cleanAlias($final_alias);

    $i++;
    $countalias = $modx->getCount("modDocument", array("alias" => $alias));
    }




    $fields = array(
    'createdby' => $modx->user->id,
    'parent' => $_POST["category"],
    'pagetitle' => $_POST["title"],
    'content' => "",
    'alias' => $alias,
    'template' => 3,
    'published' => 1,
    'isfolder' => 0,
    'context_key' => 'web'
    );

    $proccessor = $modx->runProcessor('resource/create', $fields);



    $finalprod_res = $proccessor->getObject();

    $newprodres = $modx->getObject("modResource", array("id" => $finalprod_res["id"]));


    $newprodres->set("published" , 1); // also here need to reset the published... the processor creates the resource unpublished although 'published' => 1

    $newprodres->save();




    //$uri = $newprodres->getAliasPath();


    $modx->runProcessor(array(
    'location' => 'system',
    'action' => 'clearCache',
    ));


    $newprod->set("resource_id", $newprodres->get("id"));
    $newprod->save();

    return $newprodres->get("uri"); // here we get the correct uri returned!!!! After that in every page of the site we have the problem using [[~{id}]] tag in html or the makeUrl({id}) in php!

    This question has been answered by geesmo. See the first response.

    • discuss.answer
      • 53713
      • 3 Posts
      After 3 days I found it!!!


      replace

      $modx->runProcessor(array(
      'location' => 'system',
      'action' => 'clearCache',
      ));

      with

      $modx->cacheManager->refresh();


      Cheers!!!
        • 4172
        • 5,888 Posts
        For better readability, please put code into
        [code]
        yourcode
        [/code]
        tags
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 3749
          • 24,544 Posts
          You might try $modx->reloadContext('web'), or $modx->reloadContext('mgr').

          The argument is optional. If omitted, MODX will use the current context.

          If reloadContext() works, it should be faster that cacheManager->refresh().
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 53713
            • 3 Posts
            Quote from: Bruno17 at Aug 24, 2017, 02:54 PM
            For better readability, please put code into
            [code]
            yourcode
            [/code]
            tags

            Sorry!!! Next time I will know!
              • 53932
              • 33 Posts
              $modx->cacheManager->refresh();

              I have the same issue, Thanks for this solution!! It worked like a charm!!
                • 3749
                • 24,544 Posts
                @rajani: Can you tell us which solution worked for you? There is more than one above.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 53932
                  • 33 Posts
                  Quote from: BobRay at Jan 03, 2018, 06:29 PM
                  @rajani: Can you tell us which solution worked for you? There is more than one above.

                  Problem: I have Duplicated a folder in MODX editor and updated the alias with some custom value and saved that resource. After saving the resource, unable to retrieve the URI with new alias value in some custom access.

                  Solution : Adding the "$modx->cacheManager->refresh();" after save() solved the problem.

                  $Page->set("alias", $customNewRootAlias);
                  $Page->save();
                  $modx->cacheManager->refresh();

                  Actually, I have tried the other solution given above "reloadContext()". This worked Awesome! (Noticed that reloadContext() is faster than refresh()).

                  $Page->save();
                  $modx->reloadContext();

                  Thanks Bob for nice solution. [ed. note: rajani last edited this post 6 years, 2 months ago.]
                    • 53932
                    • 33 Posts
                    Have a question here, does the below functions deletes all the cache files or specific provided set of resources? If so, how to do that? TIA.

                    $modx->cacheManager->refresh();
                    $modx->reloadContext();
                      • 53932
                      • 33 Posts
                      Have a question here, does the below functions deletes all the cache files on server or specific resources trees? TIA.

                      $modx->cacheManager->refresh();
                      $modx->reloadContext();

                      Couple of other questions in my mind that , Does reloadContext() clears only context related cache? What location the cache gets cleared? [ed. note: rajani last edited this post 6 years, 2 months ago.]