We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51048
    • 14 Posts
    Hi,

    I use Lingua tables for translations.

    below is an example of my code

    class tools {
    	
    	public static function getIdByLangCode($languageCode) {
    		
    		$modx = self::getMODX();    //this is how I get an instance of modx, working and tested
    		$lang = $modx->getObject('linguaLangs',
    		array('lcid_string' => $languageCode));
                    if (empty($lang)) {
    		     error_log('Could not find Lang');
    		     return 0;
    		}
    			
    		return $lang->get('id');
    		
    	}
    
            public static function displayAllIds() {
               $lanCodes = ("fr-fr","es-es","de-de");
               foreach ($lanCodes as $code){
                  $output .= self::getIdByLangCode($code);
               }
                return  $output;
    
           }
    
           public static function getMODX(){
    		$MODX_CORE_PATH= 'c:/wamp/www/modx/core/';
    		include_once $MODX_CORE_PATH .'model/modx/modx.class.php';
    		$modx= new modX();
    		$modx->initialize('mgr');
    		return $modx;
    	}
    }
    


    When calling displayAllIds, the first iteration works fine and was able to retrieve the correct id, but starting second iteration it fails and displays this error:

    [2015-10-20 13:35:48]  Could not get table class for class: linguaLangs
    [2015-10-20 13:35:48] Could not get table name for class: linguaLangs
    [2015-10-20 13:35:48]  Error 42000 executing statement: 
    Array
    (
        [0] => 42000
        [1] => 1064
        [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `linguaLangs` WHERE `linguaLangs`.`lcid_string` = 'es-es'' at line 1
    )


    and by the way this occurs every time I try an iteration inside my functions, first time it runs well, others fail and give the same error even with other tables.

    Need help please!

    This question has been answered by multiple community members. See the first response.

    [ed. note: mody_newbie last edited this post 10 years, 11 months ago.]
    • discuss.answer
      • 3749
      • 24,544 Posts
      I'm not sure if it will solve your problem, but the usual way to handle access to the $modx object is to create a $modx class variable and send $modx as a reference argument to the class constructor and make the methods that need it non-static.

      var $modx;
      
      __construct(&$modx) {
          $this->modx =& $modx;
      } 
      


      Then you can use $this->modx anywhere in the class.

      By using your getMODX() method, you're creating a new instance of MODX every time that method is called -- there should be only one $modx object.

      Also, if this is being used in a snippet or plugin, $modx will already be available, so there's no need to instantiate it.


        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
        • 51048
        • 14 Posts
        Quote from: BobRay at Oct 20, 2015, 11:57 PM
        I'm not sure if it will solve your problem, but the usual way to handle access to the $modx object is to create a $modx class variable and send $modx as a reference argument to the class constructor and make the methods that need it non-static.

        var $modx;
        
        __construct(&$modx) {
            $this->modx =& $modx;
        } 
        


        Then you can use $this->modx anywhere in the class.

        By using your getMODX() method, you're creating a new instance of MODX every time that method is called -- there should be only one $modx object.

        Also, if this is being used in a snippet or plugin, $modx will already be available, so there's no need to instantiate it.



        Hi BobRay,

        Thanks, didnt know the right way to do it.

        But that didnt fix my problem, the weird thing is that it works for first iteration which means all is well.

        Why would the following iterations fail?
        • discuss.answer
          • 3749
          • 24,544 Posts
          Make sure you don't have a new modX() in there somewhere.

          I don't know what else might cause that. You could try adding addPackage() or loadClass() at the top of your code or just above wherever the error is occurring.
            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
            • 51048
            • 14 Posts
            Quote from: BobRay at Oct 21, 2015, 02:11 PM
            Make sure you don't have a new modX() in there somewhere.

            I don't know what else might cause that. You could try adding addPackage() or loadClass() at the top of your code or just above wherever the error is occurring.

            Seems that was the issue, it was resolved once I make a total clean up for new modx()..

            Thanks for your help
              • 3749
              • 24,544 Posts
              I'm glad you got it sorted. Thanks for reporting back. smiley
                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