getFullTableName" in a function (scope issue?)"> getFullTableName" in a function (scope issue?)" />
We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26260
    • 12 Posts
    Hi all
    I’m trying to hack the cool "AuthorTimestamp"-snippet (http://modxcms.com/AuthorTimestamp-693.html), so it will do my bidding. But I’m running into some trouble.

    Basically I want to make the database-query-stuff into a function, so I can call it several times... here is the working code, before my hacking:
    //Get document info
    $document_data = $modx->getPageInfo($id,1,'createdby,createdon,editedby,editedon');
    
    //Get Creator details
    $db_query = $modx->db->select('fullname,email,phone,mobilephone,photo,comment', $this->getFullTableName('user_attributes'), 'id = '.$document_data['createdby']);
    $db_results = $modx->db->makeArray($db_query);
    $creator_data = $db_results[0];
    


    And now, my hacked function, derived from the above working example:
    function get_user_data($user_id) {
    	$db_query = $modx->db->select('fullname,email,phone,mobilephone,photo,comment', $this->getFullTableName('user_attributes'), 'id = '.$user_id);
    	$db_results = $modx->db->makeArray($db_query);
    	return $db_results[0];//there should be only one result!
    }
    

    ... here I feed the function with a user_id, so I can retrieve data from different users (ie. creator/editor/publisher etc.)

    This is all good... but a problem arrises around the "$this->getFullTableName" I think... anyway I’m getting following error (line 16 is the $db_query variable):
    [tt]
    Fatal error: Call to a member function on a non-object in /Library/WebServer/Documents/MODx_default/manager/includes/document.parser.class.inc.php(769) : eval()’d code on line 16
    [/tt]

    I beleive this is a scope issue... but I’m all new to php, so if anyone could point me in the right direction, I would really appreciate it.

    Thanks in advance
      Creativity is 1% inspiration and 99% perspiration.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Indeed it is a "scope" issue. In a function like that you would need to have
      global $modx;

      to let your function have access to the $modx object.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 26260
        • 12 Posts
        Thanks for the really fast reply smiley

        This seemed like a quick-fix, so I did this:
        function get_user_data($user_id) {
        	global $modx;
        	$db_query = $modx->db->select('fullname,email,phone,mobilephone,photo,comment', $this->getFullTableName('user_attributes'), 'id = '.$user_id);
        	$db_results = $modx->db->makeArray($db_query);
        	return $db_results[0];//there should be only one!
        }

        I just added your line, at the beginning of my function... but I still end up with the same Fatal Error, now on the new line 17 (still the $db_query)...

        I’m thinking: Could this stemm from somthing else, like misspellings or perhaps if I feed the function a wrong $user_id ?? (I’m asking if I should proof read the rest of my code, or if I can focus on the scope-thingy?).

          Creativity is 1% inspiration and 99% perspiration.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I think you need to change the $this to $modx. You’re not working from within the $modx object, you are accessing its function externally.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 26260
            • 12 Posts
            WOW... that did the trick...

            My new working function:
            function get_user_data($user_id) {
            	global $modx;
            	$db_query = $modx->db->select('fullname,email,phone,mobilephone,photo,comment', $modx->getFullTableName('user_attributes'), 'id = '.$user_id);
            	$db_results = $modx->db->makeArray($db_query);
            	return $db_results[0];//there should be only one!
            }

            Thanks a million... you just saved me a days worth of research and frustration!
              Creativity is 1% inspiration and 99% perspiration.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              I’ve already spent the days of frustration years ago, no point in you repeating the process. $this can be a real tongue tongue to figure out in any language, although it’s much worse in javascript.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org