We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 349
    • 76 Posts
    I need a snippet where the TVs ’Longitude’ AND ’Latitude’ are empty in the Database.
    Therefore i wrote the following snippet:
    $c = $modx->newQuery('modResource'); 
    $c->innerJoin('modTemplateVarResource','TemplateVarResources'); 
    $c->innerJoin('modTemplateVar','TemplateVar','TemplateVarResources.tmplvarid = TemplateVar.id'); 
    $c->where(array( 
    'TemplateVar.name' => 'Longitude', 
    'TemplateVarResources.value' => '0', 
    //array( 
    // 'OR:TemplateVar.name:=' => 'Latitude', 
    // 'AND:TemplateVarResources.value:=' => '0', 
    // ), 
    )); 
    
    $ress = $modx->getCollection('modResource',$c); 
    //$c->prepare(); 
    //$modx->log(xPDO::LOG_LEVEL_ERROR, $c->toSql()); 
    
    foreach($ress as $result){ 
    echo $result->get('id').'<br />'; 
    echo $result->get('pagetitle').'<br />'; 
    echo 'Longitude: '.$result->getTVValue('Longitude').'<br />'; 
    echo 'Latitude: '.$result->getTVValue('Latitude').'<br />'; 
    } 

    How do I write the correct AND condition so that ’Longitude’ AND ’Latitude’ are empty (not zero)?

    The above query works if i have a value for example of 1 and change the line
    'TemplateVarResources.value' => '0', 

    to
    'TemplateVarResources.value' => '1',


    I hope somebody can help
      • 22303 MODX Staff
      • 10,725 Posts
      If you have not set a value for a specific Resource, there will not be modTemplateVarResource record. Is your default value empty? You will not get any results when you try to join here and there is no value set for that TV on the Resource.
        • 349
        • 76 Posts
        Yes my TV default is empty. So i set it to 0 now. But how does the AND condition have to look like?
          • 7966
          • 90 Posts
          I just had the same problem. The template variable has a Default value of zero, but the field was empty in the table, site_tmplvar_contentvalues. Not sure but maybe it is a bug.

          Following is what I used that solved it for now.

          $c->where(array(
          ’context_key:=’ => "web",
          ’TemplateVarResources.value:=’ => ’’,
          ));


            • 3749
            • 24,544 Posts
            If a TVs value for a resource matches the TV’s default value, MODX doesn’t bother to store the value for that resource. When it finds that the value doesn’t exist, it pulls the default value.

            There are various ways to work around it, such as making the default value -1 or ’empty’ or some other value that will never occur in real life and testing for that value in your code, if necessary.
              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