Subscribe: RSS
  • Hello MODx community,

    I have written a snippet, to compare all alias in my db with the "check" variable. This snippet ist working, but the waiting period (if alias found) is very long. I have only 10 resource!

    Is something wrong with this snippet? Does anyone have other, faster ideas?

    $check = "home";
    $resourses = $modx->getCollection('modResource',array(
       'alias' => $check
    ));
    foreach ($resourses as $resourse) {
      $output = $resourse->get('alias');
    }
    
    if (isset($output)) {return "True";} else {return "False";}
    

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

    • I think there's an error in the code ..
      why the foreach cycle?

      what exactly does the script do?
        FerX - Developer at Eracom s.r.l.
      • The foreach cycle get the alias of all ressource. Then compare my $check value with the alias in foreach cycle.

        If $check has the same value like one of the alias, then return "true", else "false".


        I try to build error-system with this snippet: i create a new ressource with snippet and check all alias and pagetitle to avoid "double alias" or "double pagetitle.."
        • 1- modx already checks that an alias is unique.
          2- getCollection returns the resources filtered by alias, why check again?
          3- the foreach loop overwrites the variable $output

          ps: attention that it is possible to have equal aliases on different contexts

            FerX - Developer at Eracom s.r.l.

          • 1- modx already checks that an alias is unique.

            If i create ressource with snippet, then i get douplicate alias without check:

            $doc = $modx->newObject('modDocument');
                $doc->set('parent','12');
                $doc->set('pagetitle','mypagetitle');
                $doc->set('alias','myalias');
                $doc->set('template','3');
                $doc->save();
            



            2- getCollection returns the resources filtered by alias, why check again?
            But filter is not the same thing as comparison. Or have I misunderstood something?

            Sorry, i'm xPDO-beginner..


            2- getCollection returns the resources filtered by alias, why check again?
            How does he look without foreach?
            • discuss.answer
              Instead of getCollection, you could use getCount and see if you got more than 0 results. That way you can get rid of the foreach. It'd simply be like this:

              $results = $modx->getCount('modResource', ['alias' => $check]);
              return $results > 0 ? 'True' : 'False';
              



              I have no idea why your snippet call would take 10 seconds though - even with the code you posted, I'd find it hard to believe that is responsible for a 10 second delay unless you have tens of thousands of resources with the same alias... are you sure there isn't anything else happening on the page?
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with remote management features to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • @Mark: getCount is that, what im looking for - fast and simple. Thank you for this solution!


                You are right, if i run my old snippet now, i get no long waiting period more. I don't know what the problem was...


                @ferx77: thank you for your help too.