We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32098
    • 31 Posts
    I know this is a bit crazy but I am in a situation at the moment where I need a snippet that can find the id of a resource given its alias. I thought it might be possible using getResources, but the following draws a blank:

    $resources = $modx->runSnippet('getResources', array('parents' => '2', 'limit' => '1', 'where' => '{"alias:=":"rocio-villa-liebana"}'));
    $id = $resources['id'];
    return $id;


    I know in advance that the resource in question will be a child/grandchild/great-grandchild of resource 2, and I am only looking for a single result given the known alias of the resource in question (’rocio-villa-liebana’).


    Someone asked the same question a couple of years back (prior to getResources) and got an answer valid for Evolution. I wonder what the more Revolutionary answer is now?
      • 22019
      • 390 Posts
      Something like:
      $resources = $modx->getObject(modResource, array('alias' => 'rocio-villa-liebana'));


      I think. Still pretty new at this....
        Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
        • 32098
        • 31 Posts
        Thanks for the suggestion. Just tried that and got the following error message:

        Fatal error: Cannot use object of type modDocument_mysql as array in 


        Back to the drawingboard.

        Mmm!
          • 22019
          • 390 Posts
          Looks like I abbreviated too much. Here’s the related code from ’getResourceField’ snippet (which doesn’t quite do what you want, but it’s a case of subbing in the alias field, I think)
          $criteria = $modx->newQuery('modResource');
                  $criteria->select($modx->getSelectColumns('modResource','modResource','',array('id',$field)));
                  $criteria->where(array('id'=>$id,));
                  $resource = $modx->getObject('modResource',$criteria);
            Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
            • 32098
            • 31 Posts
            Odeclass, Thanks for coming back with another helping hand. I gave your second suggestion a try and got another error message, but that was probably because of a couple of lines I added to your code just to get something printed out. I realised that you can’t access the array that comes back from the query asking for something like resource[’id’]. Modx doesn’t seem to accept that.

            Anyway, after a lot of agonised searching I came back to those three articles on writing PHP in Modx in the official documentation. From that it seemed that the following very short snippet might get me a resource id just from the alias:

            $aliasv = 'rocio-villa-liebana';
            $resource =  $modx->getObject('modResource',array(   
                  'alias' => $aliasv 
              ));   
            echo $resource->get('id');
            


            When I tried that now it worked. It looks very, very simple, which makes me feel that it might actually be possible for me to get to grips with Modx.

            I think what would be really good for someone to highlight is what people need to learn (and where they can find the best educational material) to make the transition from messy old procedural PHP to the kind of stuff of which Modx is made. I’m guessing that understanding the principles of OOP might be one ’must’, so I have just ordered a book about that. But it is just a guess.
              • 32098
              • 31 Posts
              Sh*t!! Odeclas, just realised that what I posted in that last bit is exactly what you first suggested. I just messed up your excellent suggestion by adding a dumb line trying to echo $resource[’id’], which clearly is unacceptable and provokes the error message I was getting.

              You were absolutely right. Thanks.