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

    I have url-column in database table (loremipsum) with unique values. I'm trying to check if value ($_POST['url']) already exist on table. If $_POST['url'] doesn't match to database url-column, getObject should return empty - right?

    Why this is not working?

    $object = $modx->getObject('loremipsum',array('url'=>$_POST['url']));
    
    if (!$object) {
        $modx->log(modX::LOG_LEVEL_ERROR, 'empty');
    //This is not working
    } else {
        $modx->log(modX::LOG_LEVEL_ERROR, 'url exist');
    //This is working
    }


    and vice versa

    $object = $modx->getObject('loremipsum',array('url'=>$_POST['url']));
    
    if ($object) {
        $modx->log(modX::LOG_LEVEL_ERROR, 'url exist');
    //This working
    } else {
        $modx->log(modX::LOG_LEVEL_ERROR, 'empty');
    //This is not working
    }


    So i have problems to execute script if $object returns empty (or null)
      • 3749
      • 24,544 Posts
      getObject() returns null if it fails, so you can always use

      if ($object === null) 


      or

      if ($object !== null) 


      Since you're searching by $_POST['url'], is it possible that you're testing at a time when that's not set?

      Maybe doing something like this would help:

      if (isset($_POST['url']) && (!empty($_POST['url']))) {
          if ($modx->getObject('loremipsum', array('url' => $_POST['url'])) !== null) {
              /* It exists */
          } else {
              /* It doesn't exist, or the $_POST variable is not set */
          }
      }
      
        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