We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6059
    • 45 Posts
    Is there a way to check if modx->getObject is empty?

    $test = $modx->getObject(...)

    I tried if($test == null) but the if is running. If tried with count($test) but result is everytime 0 if empty or 1 if rows are returned. But with if(count($test) == 0) the if is running and with if(count(test) == 1) it is running too.

    Need help please
      • 3749
      • 24,544 Posts
      if ($test) should do it, but so should if ($test ==null).

      Is it possible that you’re retrieving a cached object when it fails to work?
        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
        • 6059
        • 45 Posts
        With if($test) and if(!$test) it is running the code inside the if. The snippet is called wth "!" so it is not cached. In Modx Revo 2.0.7 it was ok but now in 2.1.1 it is not ok. Is it a bug?
          • 6059
          • 45 Posts
          Ok found a dirty workaround

          This code is running ok

          $test = $modx->getObject('modTemplateVarResource', array(
            			'contentid'=> $id, 
            			'tmplvarid' => '14'
          		));
          
          if(!test){
             return 'run this code';
          }else{
             return 'running here';
             //If getObject Emty this code runs
          }
          
          


          and this one run not fine


          $test = $modx->getObject('modTemplateVarResource', array(
            			'contentid'=> $id, 
            			'tmplvarid' => '14'
          		));
          
          if(!test){
             return 'run this code';
             //Only run if getObject not Empty
          }
          
          


          It runs only correct with if...else but that is not ok i think. There was no Problem in modx 2.0.7

          Can someone check if i am right or help me to find a clean solution. I don´t want to write an "else" everytime, because i don´t need it.

          Thx
            • 6059
            • 45 Posts
            Git it sorry was my mistake, prog on Sunday is not good grin It was an error in my loop