We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36542
    • 3 Posts
    While using wrong value for existing table field in criteria parameter for getObject(), my local Apache server crashes, instead of retrieving null.
    For example:
    I need to get `id` of modx table `users` where `username`=`smi_23`
    $obj = $modx->getObject('modUser',array('username'=>'smi_23'));
    $res = $obj->get('id');
    

    That works fine, but if `username` equals to not existing value `smi` - the crush comes:
    $obj = $modx->getObject('modUser',array('username'=>'smi'));
    $res = $obj->get('id');
    

    The same problem appears when I using xPDOQuery:
    $q = $modx->newQuery('modUser');
    $q->where(array(
    'username'=>'smi'
    ));
    $obj = $modx->getObject('modUser',$q);
    $res = $obj->get('id');
    


    Function getCollection() retrieves correct empty array.

    If anyone have the solution or knows the reason of crash, please answer.

    Local server: Denwer 3:
    Apache 2 + SSL, PHP 5.3.1, MySQL 5.1
    [ed. note: smi_23 last edited this post 15 years, 1 month ago.]
      • 33968
      • 863 Posts
      That's because you're trying to retrieve a field from an object that does not exist.

      Try like this instead:
      $obj = $modx->getObject('modUser',array('username'=>'smi'));
      if ($obj) {
          $res = $obj->get('id');
      }
      
        • 36542
        • 3 Posts
        Thanks a lot!
        My fault, I thought that I need to check $res on existing instead of $obj...
        Thanks again