We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30223
    • 1,010 Posts
    I was trying to find a way to get the id for a page when I only know what the alias is. Browsing through the document.parser.class.inc.php I found the getDocumentObject method which I thought might do what I wanted (and a lot more). Reading through the code I noticed something I didn’t quite grasp.

    The function builds an sql statement with:

    <?php
    $tblsc = $this->getFullTableName("site_content");
    		$tbldg = $this->getFullTableName("document_groups");
    		// get document groups for current user
    		if($docgrp = $this->getUserDocGroups()) $docgrp = implode(",",$docgrp);
    		// get document
    		$access = ($this->isFrontend() ? "sc.privateweb=0":"1='".$_SESSION['mgrRole']."' OR sc.privatemgr=0").
    				  (!$docgrp ? "":" OR dg.document_group IN ($docgrp)");
    		$sql = "SELECT sc.*
    				FROM $tblsc sc
    				LEFT JOIN $tbldg dg ON dg.document = sc.id
    				WHERE sc.".$method." = '".$identifier."'
    				AND ($access) LIMIT 1;";
    		$result = $this->db->query($sql);
    		$rowCount = $this->recordCount($result);
    ?>
    


    Nothing wrong sofar but a little further on in teh code is a test that I don’t quite get. The sql is limeted by "LIMIT 1". Doesn’t that mean that the query will always return either 0 or 1 rows ($rowCount)? The test for $rowCount below baffled me:

    <?php
    		if($rowCount>1) {
    		 	// too many matches found, send the visitor to the error page
    			$this->messageQuit("More than one result returned when attempting to translate `alias` to `id` - there are multiple documents using the same alias");
    		}
    
    ?>
    

    In my mind this simply will always fail and therefore is superfluous. Isn’t it?

    Anyway, getDocumentObject() isn;t suitable for what I need because if the alias doesn’t exist or if it’s not published or not in the right document group it will redirect to an error page instead of returning an error to my snippet to be... Is there an easier way of retrieving a document’s ID from it’s alias besides rummaging through the database myself?
      • 32241
      • 1,495 Posts
      Try to print_r this variable

      print_t($modx->documentListing);
      


      I think it suppose to contain all aliases with their id.
        Wendy Novianto
        [font=Verdana]PT DJAMOER Technology Media
        [font=Verdana]Xituz Media
        • 22303 MODX Staff
        • 10,725 Posts
        That code checks to make sure there are not multiple matches of the document id in the database. If so, it reports that as an error. So if 0 records, not found or authorized, if more than 1, too many matches; 1 would be the ideal result set.
          • 30223
          • 1,010 Posts
          Quote from: OpenGeek at May 03, 2006, 01:14 PM

          That code checks to make sure there are not multiple matches of the document id in the database. If so, it reports that as an error. So if 0 records, not found or authorized, if more than 1, too many matches; 1 would be the ideal result set.

          Yes I understand the aim, but because the query has LIMIT 1 the query in this case simply can only return 0 or 1 rows. There will never be a case of more then one, unless I’ve misunderstood how mysql_num_rows works. The code would make sense without the LIMIT 1however.

          Anyway, It’s not that there’s any harm in the code I’m just trying to understand.
            • 22303 MODX Staff
            • 10,725 Posts
            Good point, I didn’t see the limit clause on first perusal. Ugh...
              • 25663 MODX Staff
              • 12,272 Posts
              Hi Toby... thanks for digging in and finding stuff like this. Could you report it in our bugtracker so we make sure it gets fixed for the next release? Thanks so much again. smiley
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                • 30223
                • 1,010 Posts
                Done -> FS#375

                In the mean time I’ve investigated this a bit more...

                It appears that getDocumentObject only is called from within the document parser class and furthermore it is always called with method ’id’. In fact it makes no sense to do it with the ’alias’ method at all as far as I can see. It is entirely possible to have several pages with the same alias (although this only makes sense when using friendly alias paths). If so, getDocumentObject would always return the same pageObject. You can easily test this as follows:

                Create a page with the alias ’test’ under root.
                Create a page in a folder, say ’testfolder’ and also give it the alias ’test’
                Now create the below snippet (call it ’textDocObject’ if you want) and place it in the body of both documents.
                <?php
                   $pageObject = $modx->getDocumentObject("alias",$modx->documentObject['alias']);
                   $output = "<p>Document ID = " . $modx->documentObject['id'] . "</p>";
                   $output .= "<p>Page Object ID  = " . $pageObject['id'] . "</p>";
                   return $output;
                ?>
                


                Make sure you have ’friendly alias’ and ’friendly alias path’ set to ’yes’ and view both pages. You’ll see that in one case it will return identical ID’s and on the other they will differ. So , even from the point of using getDocumentObject from snippets or plugins it is not useful to call it with the ’alias’ method.

                Ergo, the function could be simplified drastically without jeopardizing any existing code (I would think, because if there is code that uses it this way, eventually it would produce unwanted results) Now if the function used the modx->documentListing and was fed the whole virtual dir instead of just the alias things would be rather different. (I’d like that a lot in fact ).

                Sorry if this is getting a bit lengthy... wink
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Yeah, there’s quite a bit of that throughout the product currently. I’m certainly not satisfied with the code organization, readability, consistency, etc. That’s one reason why efforts for 1.0 release are featuring a complete rewrite of the core.

                  Also, FYI, getDocumentObject() should really be _getDocumentObject() (i.e. this is an internal method only). The fact is, alias’ are looked up and getDocumentObject is always called with the id method.
                    • 30223
                    • 1,010 Posts
                    Quote from: OpenGeek at May 04, 2006, 02:19 AM

                    Yeah, there’s quite a bit of that throughout the product currently. I’m certainly not satisfied with the code organization, readability, consistency, etc. That’s one reason why efforts for 1.0 release are featuring a complete rewrite of the core.

                    Also, FYI, getDocumentObject() should really be _getDocumentObject() (i.e. this is an internal method only). The fact is, alias’ are looked up and getDocumentObject is always called with the id method.

                    Well let me know if I can help with that in any way. I’m sure my 8 years of web development (and php) could be useful
                      • 25663 MODX Staff
                      • 12,272 Posts
                      TobyL please feel free to continue poking prodding and even offering code improvements if you feel so inclined! Thanks for being proactive. smiley
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me